Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis;
Expand All @@ -16,7 +16,7 @@ public static SyntaxNode Parentheses(this SyntaxNode syntaxNode)
return syntaxNode;
}

public static ParenthesizedExpressionSyntax Parentheses<T>(this ExpressionSyntax expression)
public static ParenthesizedExpressionSyntax Parentheses(this ExpressionSyntax expression)
{
return SyntaxFactory.ParenthesizedExpression(expression).WithAdditionalAnnotations(Simplifier.Annotation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ private static async Task<Document> RemoveComparisonWithBoolConstant(Document do

var nodeToKeep = syntaxRoot.FindNode(new TextSpan(nodeToKeepSpanStart, nodeToKeepSpanLength), getInnermostNodeForTie: true);
if (nodeToKeep.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
{
nodeToKeep = nodeToKeep.Parent;
}

if (logicalNotOperatorNeeded)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static bool TryGetReplacementExpression(SyntaxGenerator generator, IBina
otherOperand.Syntax);

replacement = binaryOperation.OperatorKind == BinaryOperatorKind.NotEquals
? SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, SyntaxFactory.ParenthesizedExpression(isNaNInvocation))
? SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, isNaNInvocation.Parentheses())
: isNaNInvocation;

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private static async Task<Document> AddComparisonOperators(Document document, Ty
AddMissingOperator("op_GreaterThan", ">", BinaryExpression(SyntaxKind.GreaterThanExpression, CreateComparerComparisonExpression(), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))));
AddMissingOperator("op_GreaterThanOrEqual", ">=", BinaryExpression(SyntaxKind.GreaterThanOrEqualExpression, CreateComparerComparisonExpression(), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))));
AddMissingOperator("op_Equality", "==", CreateEqualityComparerExpression());
AddMissingOperator("op_Inequality", "!=", PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, ParenthesizedExpression(CreateEqualityComparerExpression())));
AddMissingOperator("op_Inequality", "!=", PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, CreateEqualityComparerExpression().Parentheses()));

return editor.GetChangedDocument();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ private static async Task<Document> RemoveFinallyClause(Document document, Synta
return document;

var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
editor.ReplaceNode(tryStatement, tryStatement.WithFinally(null).WithAdditionalAnnotations(Formatter.Annotation));
if (tryStatement.Catches.Count > 0)
{
editor.ReplaceNode(tryStatement, tryStatement.WithFinally(null).WithAdditionalAnnotations(Formatter.Annotation));
}
else
{
editor.ReplaceNode(tryStatement, tryStatement.Block.WithTriviaFrom(tryStatement).WithAdditionalAnnotations(Formatter.Annotation));
}

return editor.GetChangedDocument();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Composition;
using Meziantou.Analyzer.Internals;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
Expand Down Expand Up @@ -73,7 +74,7 @@ private static async Task<Document> Update(Document document, BinaryExpressionSy
constantExpression = UnaryPattern(constantExpression);
}

var newExpression = IsPatternExpression(ParenthesizedExpression((ExpressionSyntax)expression.Syntax).WithAdditionalAnnotations(Simplifier.Annotation), constantExpression);
var newExpression = IsPatternExpression((ExpressionSyntax)expression.Syntax.Parentheses(), constantExpression);
if (newExpression is not null)
{
editor.ReplaceNode(node, newExpression);
Expand All @@ -94,6 +95,6 @@ private static async Task<Document> Update(Document document, BinaryExpressionSy
constantExpression = UnaryPattern(constantExpression);
}

return IsPatternExpression(ParenthesizedExpression(expression).WithAdditionalAnnotations(Simplifier.Annotation), constantExpression);
return IsPatternExpression(expression.Parentheses(), constantExpression);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Composition;
using Meziantou.Analyzer.Internals;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
Expand Down Expand Up @@ -100,6 +101,6 @@ private static IsPatternExpressionSyntax MakeIsNotNull(ExpressionSyntax instance
constantExpression = UnaryPattern(constantExpression);
}

return IsPatternExpression(ParenthesizedExpression(instance).WithAdditionalAnnotations(Simplifier.Annotation), constantExpression);
return IsPatternExpression(instance.Parentheses(), constantExpression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Test
{
void A(float value)
{
_ = !(float.IsNaN(value));
_ = !float.IsNaN(value);
}
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task MA0097_CodeFix()
{
var originalCode = """
using System;

class {|MA0097:Test|} : IComparable<Test>, IEquatable<Test>
{
public int CompareTo(Test other) => throw null;
Expand All @@ -54,7 +54,7 @@ class {|MA0097:Test|} : IComparable<Test>, IEquatable<Test>
""";
var fixedCode = """
using System;

class Test : IComparable<Test>, IEquatable<Test>
{
public int CompareTo(Test other) => throw null;
Expand All @@ -67,7 +67,7 @@ class Test : IComparable<Test>, IEquatable<Test>
public static bool operator >(Test left, Test right) => System.Collections.Generic.Comparer<Test>.Default.Compare(left, right) > 0;
public static bool operator >=(Test left, Test right) => System.Collections.Generic.Comparer<Test>.Default.Compare(left, right) >= 0;
public static bool operator ==(Test left, Test right) => System.Collections.Generic.EqualityComparer<Test>.Default.Equals(left, right);
public static bool operator !=(Test left, Test right) => !(System.Collections.Generic.EqualityComparer<Test>.Default.Equals(left, right));
public static bool operator !=(Test left, Test right) => !System.Collections.Generic.EqualityComparer<Test>.Default.Equals(left, right);
}
""";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,54 @@ void A()
""";

const string FixedCode = """
class Test
{
void A()
{
{
}
}
}
""";

await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.ShouldFixCodeWith(FixedCode)
.ValidateAsync();
}

[Fact]
public async Task EmptyFinallyBlock_WithCatch_CodeFix()
{
const string SourceCode = """
class Test
{
void A()
{
try
{
}
catch
{
}
[|finally
{
}|]
}
}
""";

const string FixedCode = """
class Test
{
void A()
{
try
{
}
catch
{
}
}
}
""";
Expand Down
Loading