Skip to content

Commit

Permalink
Update SA1010 to not trigger on list patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed May 16, 2022
1 parent 8c609a7 commit 7e25f23
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,47 @@

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
{
[Fact]
[WorkItem(3503, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3503")]
public async Task TestListPatternAsync()
{
var testCode = @"
using System.Collections.Generic;
namespace TestNamespace
{
public class TestClass
{
public void TestMethod(List<int> x)
{
_ = x is [1];
_ = x is not [1];
_ = x is ([1] or [2]);
_ = x is ([1] or not [2]);
_ = x is ([1] and [1]);
_ = x is ([1] and not [2]);
}
}
}
";

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 7e25f23

Please sign in to comment.