Skip to content

Commit d22f8a0

Browse files
committed
fixed #3904
Collectionexpressions are detected as variable declarations and not as InitializerExpression - Filter based on the declared variables collectionexpressions out, and then check each element
1 parent f5843ae commit d22f8a0

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

Diff for: StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/ReadabilityRules/SA1137CSharp12UnitTests.cs

+52
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,61 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp12.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp11.ReadabilityRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopDiagnosticVerifier<
12+
StyleCop.Analyzers.ReadabilityRules.SA1137ElementsShouldHaveTheSameIndentation>;
713

814
public partial class SA1137CSharp12UnitTests : SA1137CSharp11UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3904, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3904")]
18+
public async Task TestCollectionInitializationAsync()
19+
{
20+
var csharp = new CSharpTest()
21+
{
22+
TestSources =
23+
{
24+
@"
25+
class FirstCase
26+
{
27+
private readonly System.Collections.Generic.List<string> example1 =
28+
[
29+
""a"",
30+
""b"",
31+
""c"",
32+
];
33+
}",
34+
@"
35+
class SecondCase
36+
{
37+
private readonly System.Collections.Generic.List<int> example2 =
38+
[
39+
1,
40+
2,
41+
3,
42+
4,
43+
];
44+
}",
45+
@"
46+
class ThirdCase
47+
{
48+
private readonly System.Collections.Generic.List<int> example3 = [1, 2, 3, 4];
49+
}",
50+
},
51+
ExpectedDiagnostics =
52+
{
53+
DiagnosticResult.CompilerWarning("SA1137").WithLocation("/0/Test0.cs", 7, 1),
54+
DiagnosticResult.CompilerWarning("SA1137").WithLocation("/0/Test0.cs", 8, 1),
55+
DiagnosticResult.CompilerWarning("SA1137").WithLocation("/0/Test1.cs", 8, 1),
56+
DiagnosticResult.CompilerWarning("SA1137").WithLocation("/0/Test1.cs", 9, 1),
57+
},
58+
};
59+
60+
await csharp.RunAsync(CancellationToken.None).ConfigureAwait(false);
61+
}
1062
}
1163
}

Diff for: StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs

+23
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ private static void HandleVariableDeclaration(SyntaxNodeAnalysisContext context)
154154
{
155155
var variableDeclaration = (VariableDeclarationSyntax)context.Node;
156156
CheckElements(context, variableDeclaration.Variables);
157+
158+
var collectionsExpressions = variableDeclaration.Variables
159+
.Where(variable => IsCollectionExpression(variable))
160+
.Select(filtered => filtered.Initializer.Value)
161+
.ToList();
162+
163+
foreach (var expression in collectionsExpressions)
164+
{
165+
var childNodes = expression.ChildNodes().ToImmutableList();
166+
CheckElements(context, childNodes);
167+
}
157168
}
158169

159170
private static void HandleTypeParameterList(SyntaxNodeAnalysisContext context)
@@ -539,5 +550,17 @@ private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, SyntaxTo
539550
ImmutableDictionary<string, string> properties = ImmutableDictionary.Create<string, string>().SetItem(ExpectedIndentationKey, expectedIndentation);
540551
context.ReportDiagnostic(Diagnostic.Create(Descriptor, location, properties));
541552
}
553+
554+
private static bool IsCollectionExpression(VariableDeclaratorSyntax declarationSyntax)
555+
{
556+
if (declarationSyntax.Initializer?.Value == null)
557+
{
558+
return false;
559+
}
560+
561+
// directly check for rawkind, to avoid double casting when using IsKind(SyntaxKind)
562+
// ID is taken from https://source.dot.net/#Microsoft.CodeAnalysis.CSharp/Syntax/SyntaxKind.cs,925
563+
return declarationSyntax.Initializer.Value.RawKind == 9076;
564+
}
542565
}
543566
}

0 commit comments

Comments
 (0)