Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SA1137 is not raised #3910

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}