Skip to content

Commit

Permalink
fixed #3904
Browse files Browse the repository at this point in the history
Collectionexpressions are detected as variable declarations and not as InitializerExpression

- Filter based on the declared variables collectionexpressions out, and then check each element
  • Loading branch information
Mivee committed Dec 20, 2024
1 parent f5843ae commit d22f8a0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> example1 =
[
""a"",
""b"",
""c"",
];
}",
@"
class SecondCase
{
private readonly System.Collections.Generic.List<int> example2 =
[
1,
2,
3,
4,
];
}",
@"
class ThirdCase
{
private readonly System.Collections.Generic.List<int> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -539,5 +550,17 @@ private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, SyntaxTo
ImmutableDictionary<string, string> properties = ImmutableDictionary.Create<string, string>().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;
}
}
}

0 comments on commit d22f8a0

Please sign in to comment.