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

Update SA1010 to not trigger on list patterns #3507

Merged
merged 1 commit into from
Apr 25, 2023
Merged
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,48 @@

namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;

using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1010OpeningSquareBracketsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public class SA1010CSharp11UnitTests : SA1010CSharp10UnitTests
{
[Theory]
[InlineData("x is [1]")]
[InlineData("x is not [1]")]
[InlineData("x is ([1] or [2])")]
[InlineData("x is ([1] or not [2])")]
[InlineData("x is ([1] and [1])")]
[InlineData("x is ([1] and not [2])")]
[WorkItem(3503, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3503")]
public async Task TestListPatternAsync(string condition)
{
var testCode = $@"
using System.Collections.Generic;

namespace TestNamespace
{{
public class TestClass
{{
public void TestMethod(List<int> x)
{{
_ = {condition};
}}
}}
}}
";

await new CSharpTest()
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.SpacingRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;

/// <summary>
/// An opening square bracket within a C# statement is not spaced correctly.
Expand Down Expand Up @@ -98,15 +99,16 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy
}
}

bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();

if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem && !IsPartOfIndexInitializer(token))
if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem &&
!IsPartOfIndexInitializer(token) && !IsPartOfListPattern(token))
{
// Opening square bracket should {not be preceded} by a space.
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotPreceded, token.GetLocation(), TokenSpacingProperties.RemovePreceding));
}

bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();

if (!lastInLine && followedBySpace)
{
// Opening square bracket should {not be followed} by a space.
Expand All @@ -121,5 +123,10 @@ private static bool IsPartOfIndexInitializer(SyntaxToken token)
return token.Parent.IsKind(SyntaxKind.BracketedArgumentList)
&& token.Parent.Parent.IsKind(SyntaxKind.ImplicitElementAccess);
}

private static bool IsPartOfListPattern(SyntaxToken token)
{
return token.Parent.IsKind(SyntaxKindEx.ListPattern);
}
}
}