From e72fa1c659103af3d35785c30e94262eaf96f46a Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Mon, 26 Feb 2024 18:24:03 +0100 Subject: [PATCH] Review 2 --- .../Extensions/SyntaxNodeExtensions.cs | 22 +++++++++++++------ .../Extensions/SyntaxNodeExtensionsTest.cs | 2 ++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs b/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs index 2fa4d3d76eb..4dfe05a601d 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs @@ -26,12 +26,13 @@ internal static partial class SyntaxNodeExtensions { private static readonly ControlFlowGraphCache CfgCache = new(); private static readonly SyntaxKind[] ParenthesizedNodeKinds = [SyntaxKind.ParenthesizedExpression, SyntaxKindEx.ParenthesizedPattern]; + private static readonly SyntaxKind[] EnclosingScopeSyntaxKinds = [ - SyntaxKind.AddAccessorDeclaration, SyntaxKind.AddAccessorDeclaration, SyntaxKind.AnonymousMethodExpression, SyntaxKind.BaseConstructorInitializer, SyntaxKind.ConstructorDeclaration, + SyntaxKind.ConversionOperatorDeclaration, SyntaxKind.DestructorDeclaration, SyntaxKind.EqualsValueClause, SyntaxKind.GetAccessorDeclaration, @@ -39,6 +40,7 @@ internal static partial class SyntaxNodeExtensions SyntaxKindEx.InitAccessorDeclaration, SyntaxKindEx.LocalFunctionStatement, SyntaxKind.MethodDeclaration, + SyntaxKind.OperatorDeclaration, SyntaxKind.ParenthesizedLambdaExpression, SyntaxKindEx.PrimaryConstructorBaseType, SyntaxKind.RemoveAccessorDeclaration, @@ -46,6 +48,16 @@ internal static partial class SyntaxNodeExtensions SyntaxKind.SimpleLambdaExpression, SyntaxKind.ThisConstructorInitializer]; + private static readonly SyntaxKind[] NegationOrConditionEnclosingSyntaxKinds = [ + SyntaxKind.AnonymousMethodExpression, + SyntaxKind.BitwiseNotExpression, + SyntaxKind.ConditionalExpression, + SyntaxKind.IfStatement, + SyntaxKind.MethodDeclaration, + SyntaxKind.ParenthesizedLambdaExpression, + SyntaxKind.SimpleLambdaExpression, + SyntaxKind.WhileStatement]; + public static ControlFlowGraph CreateCfg(this SyntaxNode node, SemanticModel model, CancellationToken cancel) => CfgCache.FindOrCreate(node, model, cancel); @@ -79,12 +91,8 @@ public static bool IsPartOfBinaryNegationOrCondition(this SyntaxNode node) } var current = topNode; - while (current.Parent != null && current.Parent?.Kind() is not (SyntaxKind.BitwiseNotExpression - or SyntaxKind.IfStatement - or SyntaxKind.WhileStatement - or SyntaxKind.ConditionalExpression - or SyntaxKind.MethodDeclaration - or SyntaxKind.SimpleLambdaExpression)) + while (current.Parent != null + && !NegationOrConditionEnclosingSyntaxKinds.Contains(current.Parent.Kind())) { current = current.Parent; } diff --git a/analyzers/tests/SonarAnalyzer.Test/Extensions/SyntaxNodeExtensionsTest.cs b/analyzers/tests/SonarAnalyzer.Test/Extensions/SyntaxNodeExtensionsTest.cs index 7f8a6c62773..27f024af618 100644 --- a/analyzers/tests/SonarAnalyzer.Test/Extensions/SyntaxNodeExtensionsTest.cs +++ b/analyzers/tests/SonarAnalyzer.Test/Extensions/SyntaxNodeExtensionsTest.cs @@ -1205,6 +1205,7 @@ public class Derived(int i) {{baseType}} { } [DataRow("""int Method() { Func add = delegate (int a, int b) { return $$a + b$$; }; return add(1, 2); }""", SyntaxKind.AnonymousMethodExpression)] [DataRow("""Derived(int arg) : base($$arg$$) { }""", SyntaxKind.BaseConstructorInitializer)] [DataRow("""Derived() { $$var x = 42;$$ }""", SyntaxKind.ConstructorDeclaration)] + [DataRow("""public static implicit operator int(Derived d) => $$42$$;""", SyntaxKind.ConversionOperatorDeclaration)] [DataRow("""~Derived() { $$var x = 42;$$ }""", SyntaxKind.DestructorDeclaration)] [DataRow("""int field = $$int.Parse("42")$$;""", SyntaxKind.EqualsValueClause)] [DataRow("""int Property { get; set; } = $$int.Parse("42")$$;""", SyntaxKind.EqualsValueClause)] @@ -1214,6 +1215,7 @@ public class Derived(int i) {{baseType}} { } [DataRow("""int Method() { return LocalFunction(); int LocalFunction() => $$42$$; }""", SyntaxKindEx.LocalFunctionStatement)] [DataRow("""int Method() { $$return 42;$$ }""", SyntaxKind.MethodDeclaration)] [DataRow("""int Method() => $$42$$;""", SyntaxKind.MethodDeclaration)] + [DataRow("""public static Derived operator +(Derived d) => $$d$$;""", SyntaxKind.OperatorDeclaration)] [DataRow("""int Method() { var lambda = () => $$42$$; return lambda(); }""", SyntaxKind.ParenthesizedLambdaExpression)] [DataRow("""int Method() { Func lambda = x => $$x + 1$$; return lambda(42); }""", SyntaxKind.SimpleLambdaExpression)] [DataRow("""event EventHandler SomeEvent { add { int x = 42; } remove { $$int x = 42;$$ } }""", SyntaxKind.RemoveAccessorDeclaration)]