Skip to content

Commit

Permalink
Fix MA0141 fixer to preserve parentheses when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Nov 13, 2023
1 parent a5b7251 commit b1e7508
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Simplification;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Meziantou.Analyzer.Rules;
Expand Down Expand Up @@ -51,7 +52,7 @@ private static async Task<Document> Update(Document document, BinaryExpressionSy
constantExpression = UnaryPattern(constantExpression);
}

var newSyntax = IsPatternExpression(expression, constantExpression);
var newSyntax = IsPatternExpression(ParenthesizedExpression(expression).WithAdditionalAnnotations(Simplifier.Annotation), constantExpression);
editor.ReplaceNode(node, newSyntax);
return editor.GetChangedDocument();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ await CreateProjectBuilder()
.ShouldFixCodeWith("_ = new object() is not null;")
.ValidateAsync();
}

[Fact]
public async Task NullCheckForObject_FixerKeepParentheses()
{
await CreateProjectBuilder()
.WithSourceCode("""
string line;
while ([|(line = null) != null|]) { }
""")
.ShouldFixCodeWith("""
string line;
while ((line = null) is not null) { }
""")
.ValidateAsync();
}

[Fact]
public async Task NullEqualsNull()
Expand Down

0 comments on commit b1e7508

Please sign in to comment.