From d22f8a0fab3f97d417b87c4ceed4a433a25f7f11 Mon Sep 17 00:00:00 2001 From: Mivee Date: Sun, 15 Dec 2024 00:41:20 +0100 Subject: [PATCH] fixed https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3904 Collectionexpressions are detected as variable declarations and not as InitializerExpression - Filter based on the declared variables collectionexpressions out, and then check each element --- .../SA1137CSharp12UnitTests.cs | 52 +++++++++++++++++++ ...137ElementsShouldHaveTheSameIndentation.cs | 23 ++++++++ 2 files changed, 75 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/ReadabilityRules/SA1137CSharp12UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/ReadabilityRules/SA1137CSharp12UnitTests.cs index 32aa35907..9044f710a 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/ReadabilityRules/SA1137CSharp12UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/ReadabilityRules/SA1137CSharp12UnitTests.cs @@ -3,9 +3,61 @@ namespace StyleCop.Analyzers.Test.CSharp12.ReadabilityRules { + using System.Threading; + using System.Threading.Tasks; + using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp11.ReadabilityRules; + using Xunit; + using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier< + StyleCop.Analyzers.ReadabilityRules.SA1137ElementsShouldHaveTheSameIndentation>; public partial class SA1137CSharp12UnitTests : SA1137CSharp11UnitTests { + [Fact] + [WorkItem(3904, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3904")] + public async Task TestCollectionInitializationAsync() + { + var csharp = new CSharpTest() + { + TestSources = + { + @" + class FirstCase + { + private readonly System.Collections.Generic.List example1 = + [ + ""a"", + ""b"", + ""c"", + ]; + }", + @" + class SecondCase + { + private readonly System.Collections.Generic.List example2 = + [ + 1, + 2, + 3, + 4, + ]; + }", + @" + class ThirdCase + { + private readonly System.Collections.Generic.List example3 = [1, 2, 3, 4]; + }", + }, + ExpectedDiagnostics = + { + DiagnosticResult.CompilerWarning("SA1137").WithLocation("/0/Test0.cs", 7, 1), + DiagnosticResult.CompilerWarning("SA1137").WithLocation("/0/Test0.cs", 8, 1), + DiagnosticResult.CompilerWarning("SA1137").WithLocation("/0/Test1.cs", 8, 1), + DiagnosticResult.CompilerWarning("SA1137").WithLocation("/0/Test1.cs", 9, 1), + }, + }; + + await csharp.RunAsync(CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs index ec5700d2f..748381284 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs @@ -154,6 +154,17 @@ private static void HandleVariableDeclaration(SyntaxNodeAnalysisContext context) { var variableDeclaration = (VariableDeclarationSyntax)context.Node; CheckElements(context, variableDeclaration.Variables); + + var collectionsExpressions = variableDeclaration.Variables + .Where(variable => IsCollectionExpression(variable)) + .Select(filtered => filtered.Initializer.Value) + .ToList(); + + foreach (var expression in collectionsExpressions) + { + var childNodes = expression.ChildNodes().ToImmutableList(); + CheckElements(context, childNodes); + } } private static void HandleTypeParameterList(SyntaxNodeAnalysisContext context) @@ -539,5 +550,17 @@ private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, SyntaxTo ImmutableDictionary properties = ImmutableDictionary.Create().SetItem(ExpectedIndentationKey, expectedIndentation); context.ReportDiagnostic(Diagnostic.Create(Descriptor, location, properties)); } + + private static bool IsCollectionExpression(VariableDeclaratorSyntax declarationSyntax) + { + if (declarationSyntax.Initializer?.Value == null) + { + return false; + } + + // directly check for rawkind, to avoid double casting when using IsKind(SyntaxKind) + // ID is taken from https://source.dot.net/#Microsoft.CodeAnalysis.CSharp/Syntax/SyntaxKind.cs,925 + return declarationSyntax.Initializer.Value.RawKind == 9076; + } } }