From 8f64beae5b8fe8f2e80c4e506fc628dcfdc6836c Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Mon, 24 Jul 2023 18:16:44 +0200 Subject: [PATCH 1/4] Move stringliteral tokens array to static field --- .../Rules/Utilities/TokenTypeAnalyzer.cs | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/Utilities/TokenTypeAnalyzer.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/Utilities/TokenTypeAnalyzer.cs index 299cc07b3ad..44bc008672b 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/Utilities/TokenTypeAnalyzer.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/Utilities/TokenTypeAnalyzer.cs @@ -33,6 +33,24 @@ protected override TriviaClassifierBase GetTriviaClassifier() => private sealed class TokenClassifier : TokenClassifierBase { + private static readonly SyntaxKind[] StringLiteralTokens = new[] + { + SyntaxKind.StringLiteralToken, + SyntaxKind.CharacterLiteralToken, + SyntaxKindEx.SingleLineRawStringLiteralToken, + SyntaxKindEx.MultiLineRawStringLiteralToken, + SyntaxKindEx.Utf8StringLiteralToken, + SyntaxKindEx.Utf8SingleLineRawStringLiteralToken, + SyntaxKindEx.Utf8MultiLineRawStringLiteralToken, + SyntaxKind.InterpolatedStringStartToken, + SyntaxKind.InterpolatedVerbatimStringStartToken, + SyntaxKindEx.InterpolatedSingleLineRawStringStartToken, + SyntaxKindEx.InterpolatedMultiLineRawStringStartToken, + SyntaxKind.InterpolatedStringTextToken, + SyntaxKind.InterpolatedStringEndToken, + SyntaxKindEx.InterpolatedRawStringEndToken + }; + public TokenClassifier(SemanticModel semanticModel, bool skipIdentifiers) : base(semanticModel, skipIdentifiers) { } protected override SyntaxNode GetBindableParent(SyntaxToken token) => @@ -48,21 +66,7 @@ protected override bool IsNumericLiteral(SyntaxToken token) => token.IsKind(SyntaxKind.NumericLiteralToken); protected override bool IsStringLiteral(SyntaxToken token) => - token.IsAnyKind( - SyntaxKind.StringLiteralToken, - SyntaxKind.CharacterLiteralToken, - SyntaxKindEx.SingleLineRawStringLiteralToken, - SyntaxKindEx.MultiLineRawStringLiteralToken, - SyntaxKindEx.Utf8StringLiteralToken, - SyntaxKindEx.Utf8SingleLineRawStringLiteralToken, - SyntaxKindEx.Utf8MultiLineRawStringLiteralToken, - SyntaxKind.InterpolatedStringStartToken, - SyntaxKind.InterpolatedVerbatimStringStartToken, - SyntaxKindEx.InterpolatedSingleLineRawStringStartToken, - SyntaxKindEx.InterpolatedMultiLineRawStringStartToken, - SyntaxKind.InterpolatedStringTextToken, - SyntaxKind.InterpolatedStringEndToken, - SyntaxKindEx.InterpolatedRawStringEndToken); + token.IsAnyKind(StringLiteralTokens); } private sealed class TriviaClassifier : TriviaClassifierBase From 375f4e742fc83fff5666b49bced399ba3c463ce0 Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Mon, 24 Jul 2023 18:35:14 +0200 Subject: [PATCH 2/4] Move comment syntax kinds to static array --- .../Rules/Utilities/TokenTypeAnalyzer.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/Utilities/TokenTypeAnalyzer.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/Utilities/TokenTypeAnalyzer.cs index 44bc008672b..f709ab9db93 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/Utilities/TokenTypeAnalyzer.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/Utilities/TokenTypeAnalyzer.cs @@ -48,7 +48,7 @@ private sealed class TokenClassifier : TokenClassifierBase SyntaxKindEx.InterpolatedMultiLineRawStringStartToken, SyntaxKind.InterpolatedStringTextToken, SyntaxKind.InterpolatedStringEndToken, - SyntaxKindEx.InterpolatedRawStringEndToken + SyntaxKindEx.InterpolatedRawStringEndToken, }; public TokenClassifier(SemanticModel semanticModel, bool skipIdentifiers) : base(semanticModel, skipIdentifiers) { } @@ -71,11 +71,23 @@ protected override bool IsStringLiteral(SyntaxToken token) => private sealed class TriviaClassifier : TriviaClassifierBase { + private static readonly SyntaxKind[] RegularCommentToken = new[] + { + SyntaxKind.SingleLineCommentTrivia, + SyntaxKind.MultiLineCommentTrivia, + }; + + private static readonly SyntaxKind[] DocCommentToken = new[] + { + SyntaxKind.SingleLineDocumentationCommentTrivia, + SyntaxKind.MultiLineDocumentationCommentTrivia, + }; + protected override bool IsRegularComment(SyntaxTrivia trivia) => - trivia.IsAnyKind(SyntaxKind.SingleLineCommentTrivia, SyntaxKind.MultiLineCommentTrivia); + trivia.IsAnyKind(RegularCommentToken); protected override bool IsDocComment(SyntaxTrivia trivia) => - trivia.IsAnyKind(SyntaxKind.SingleLineDocumentationCommentTrivia, SyntaxKind.MultiLineDocumentationCommentTrivia); + trivia.IsAnyKind(DocCommentToken); } } } From b1d32fc84f143578c620c529dc33e2308409cf27 Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Mon, 24 Jul 2023 18:38:41 +0200 Subject: [PATCH 3/4] SyntaxKind array for RemoveParentheses --- .../SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs b/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs index bb6d753d912..df65013f04e 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs @@ -25,6 +25,7 @@ namespace SonarAnalyzer.Extensions internal static partial class SyntaxNodeExtensions { private static readonly ControlFlowGraphCache CfgCache = new(); + private static readonly SyntaxKind[] ParenthesizedNodeKinds = new[] { SyntaxKind.ParenthesizedExpression, SyntaxKindEx.ParenthesizedPattern }; public static ControlFlowGraph CreateCfg(this SyntaxNode body, SemanticModel model, CancellationToken cancel) => CfgCache.FindOrCreate(body is CompilationUnitSyntax ? body : body.Parent, model, cancel); @@ -115,8 +116,9 @@ public static ArrowExpressionClauseSyntax ArrowExpressionBody(this SyntaxNode no public static SyntaxNode RemoveParentheses(this SyntaxNode expression) { + var current = expression; - while (current is { } && current.IsAnyKind(SyntaxKind.ParenthesizedExpression, SyntaxKindEx.ParenthesizedPattern)) + while (current is { } && current.IsAnyKind(ParenthesizedNodeKinds)) { current = current.IsKind(SyntaxKindEx.ParenthesizedPattern) ? ((ParenthesizedPatternSyntaxWrapper)current).Pattern From a4041834a5ad3790fda0e7b8b0fe9cdbfe0736fe Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Tue, 25 Jul 2023 12:27:55 +0200 Subject: [PATCH 4/4] Remove newLine --- .../src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs b/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs index df65013f04e..9f76fd6f828 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Extensions/SyntaxNodeExtensions.cs @@ -116,7 +116,6 @@ public static ArrowExpressionClauseSyntax ArrowExpressionBody(this SyntaxNode no public static SyntaxNode RemoveParentheses(this SyntaxNode expression) { - var current = expression; while (current is { } && current.IsAnyKind(ParenthesizedNodeKinds)) {