Skip to content

Commit

Permalink
Merge pull request #3507 from bjornhellander/feature/listpattern
Browse files Browse the repository at this point in the history
Update SA1010 to not trigger on list patterns
  • Loading branch information
sharwell authored Apr 25, 2023
2 parents e470584 + 94243e6 commit 1c7f5f3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
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);
}
}
}

0 comments on commit 1c7f5f3

Please sign in to comment.