diff --git a/src/Meziantou.Analyzer.CodeFixers/Internals/ParenthesesExtensions.cs b/src/Meziantou.Analyzer.CodeFixers/Internals/ParenthesesExtensions.cs index 37baa65f0..b06ca9992 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Internals/ParenthesesExtensions.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Internals/ParenthesesExtensions.cs @@ -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; @@ -16,7 +16,7 @@ public static SyntaxNode Parentheses(this SyntaxNode syntaxNode) return syntaxNode; } - public static ParenthesizedExpressionSyntax Parentheses(this ExpressionSyntax expression) + public static ParenthesizedExpressionSyntax Parentheses(this ExpressionSyntax expression) { return SyntaxFactory.ParenthesizedExpression(expression).WithAdditionalAnnotations(Simplifier.Annotation); } diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/AvoidComparisonWithBoolConstantFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/AvoidComparisonWithBoolConstantFixer.cs index 3fbf2e36a..b479e01e1 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/AvoidComparisonWithBoolConstantFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/AvoidComparisonWithBoolConstantFixer.cs @@ -50,7 +50,9 @@ private static async Task RemoveComparisonWithBoolConstant(Document do var nodeToKeep = syntaxRoot.FindNode(new TextSpan(nodeToKeepSpanStart, nodeToKeepSpanLength), getInnermostNodeForTie: true); if (nodeToKeep.Parent.IsKind(SyntaxKind.ParenthesizedExpression)) + { nodeToKeep = nodeToKeep.Parent; + } if (logicalNotOperatorNeeded) { diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/DoNotNaNInComparisonsFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/DoNotNaNInComparisonsFixer.cs index 53336627e..14858b2f4 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/DoNotNaNInComparisonsFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/DoNotNaNInComparisonsFixer.cs @@ -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; diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/EqualityShouldBeCorrectlyImplementedFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/EqualityShouldBeCorrectlyImplementedFixer.cs index 30c534fe7..abeb255e9 100755 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/EqualityShouldBeCorrectlyImplementedFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/EqualityShouldBeCorrectlyImplementedFixer.cs @@ -282,7 +282,7 @@ private static async Task 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(); diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/RemoveEmptyBlockFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/RemoveEmptyBlockFixer.cs index 64eaf6fbc..1bccc7921 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/RemoveEmptyBlockFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/RemoveEmptyBlockFixer.cs @@ -62,7 +62,15 @@ private static async Task 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(); } } diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/UsePatternMatchingForEqualityComparisonsFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/UsePatternMatchingForEqualityComparisonsFixer.cs index f8061ef3a..abbfce08f 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/UsePatternMatchingForEqualityComparisonsFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/UsePatternMatchingForEqualityComparisonsFixer.cs @@ -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; @@ -73,7 +74,7 @@ private static async Task 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); @@ -94,6 +95,6 @@ private static async Task Update(Document document, BinaryExpressionSy constantExpression = UnaryPattern(constantExpression); } - return IsPatternExpression(ParenthesizedExpression(expression).WithAdditionalAnnotations(Simplifier.Annotation), constantExpression); + return IsPatternExpression(expression.Parentheses(), constantExpression); } } diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/UsePatternMatchingInsteadOfHasvalueFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/UsePatternMatchingInsteadOfHasvalueFixer.cs index b68510ce8..2b772cf17 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/UsePatternMatchingInsteadOfHasvalueFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/UsePatternMatchingInsteadOfHasvalueFixer.cs @@ -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; @@ -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); } } diff --git a/tests/Meziantou.Analyzer.Test/Rules/DoNotNaNInComparisonsAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/DoNotNaNInComparisonsAnalyzerTests.cs index be267ca86..3847386f1 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/DoNotNaNInComparisonsAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/DoNotNaNInComparisonsAnalyzerTests.cs @@ -97,7 +97,7 @@ class Test { void A(float value) { - _ = !(float.IsNaN(value)); + _ = !float.IsNaN(value); } } """; diff --git a/tests/Meziantou.Analyzer.Test/Rules/EqualityShouldBeCorrectlyImplementedAnalyzerMA0097Tests.cs b/tests/Meziantou.Analyzer.Test/Rules/EqualityShouldBeCorrectlyImplementedAnalyzerMA0097Tests.cs index 9a4d78612..cf3bf218d 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/EqualityShouldBeCorrectlyImplementedAnalyzerMA0097Tests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/EqualityShouldBeCorrectlyImplementedAnalyzerMA0097Tests.cs @@ -43,7 +43,7 @@ public async Task MA0097_CodeFix() { var originalCode = """ using System; - + class {|MA0097:Test|} : IComparable, IEquatable { public int CompareTo(Test other) => throw null; @@ -54,7 +54,7 @@ class {|MA0097:Test|} : IComparable, IEquatable """; var fixedCode = """ using System; - + class Test : IComparable, IEquatable { public int CompareTo(Test other) => throw null; @@ -67,7 +67,7 @@ class Test : IComparable, IEquatable public static bool operator >(Test left, Test right) => System.Collections.Generic.Comparer.Default.Compare(left, right) > 0; public static bool operator >=(Test left, Test right) => System.Collections.Generic.Comparer.Default.Compare(left, right) >= 0; public static bool operator ==(Test left, Test right) => System.Collections.Generic.EqualityComparer.Default.Equals(left, right); - public static bool operator !=(Test left, Test right) => !(System.Collections.Generic.EqualityComparer.Default.Equals(left, right)); + public static bool operator !=(Test left, Test right) => !System.Collections.Generic.EqualityComparer.Default.Equals(left, right); } """; diff --git a/tests/Meziantou.Analyzer.Test/Rules/RemoveEmptyBlockAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/RemoveEmptyBlockAnalyzerTests.cs index dea8be0b1..6e2337167 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/RemoveEmptyBlockAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/RemoveEmptyBlockAnalyzerTests.cs @@ -208,6 +208,26 @@ 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() @@ -215,6 +235,27 @@ void A() try { } + catch + { + } + [|finally + { + }|] + } + } + """; + + const string FixedCode = """ + class Test + { + void A() + { + try + { + } + catch + { + } } } """;