Skip to content

Commit 94243e6

Browse files
Update SA1010 to not trigger on list patterns
1 parent ccaac20 commit 94243e6

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1010CSharp11UnitTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,48 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
9+
using StyleCop.Analyzers.Test.Verifiers;
10+
using Xunit;
11+
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.SpacingRules.SA1010OpeningSquareBracketsMustBeSpacedCorrectly,
14+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
715

816
public class SA1010CSharp11UnitTests : SA1010CSharp10UnitTests
917
{
18+
[Theory]
19+
[InlineData("x is [1]")]
20+
[InlineData("x is not [1]")]
21+
[InlineData("x is ([1] or [2])")]
22+
[InlineData("x is ([1] or not [2])")]
23+
[InlineData("x is ([1] and [1])")]
24+
[InlineData("x is ([1] and not [2])")]
25+
[WorkItem(3503, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3503")]
26+
public async Task TestListPatternAsync(string condition)
27+
{
28+
var testCode = $@"
29+
using System.Collections.Generic;
30+
31+
namespace TestNamespace
32+
{{
33+
public class TestClass
34+
{{
35+
public void TestMethod(List<int> x)
36+
{{
37+
_ = {condition};
38+
}}
39+
}}
40+
}}
41+
";
42+
43+
await new CSharpTest()
44+
{
45+
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
46+
TestCode = testCode,
47+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
48+
}
1049
}
1150
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.SpacingRules
1111
using Microsoft.CodeAnalysis.CSharp;
1212
using Microsoft.CodeAnalysis.Diagnostics;
1313
using StyleCop.Analyzers.Helpers;
14+
using StyleCop.Analyzers.Lightup;
1415

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

101-
bool followedBySpace = token.IsFollowedByWhitespace();
102-
bool lastInLine = token.IsLastInLine();
103-
104-
if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem && !IsPartOfIndexInitializer(token))
102+
if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem &&
103+
!IsPartOfIndexInitializer(token) && !IsPartOfListPattern(token))
105104
{
106105
// Opening square bracket should {not be preceded} by a space.
107106
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotPreceded, token.GetLocation(), TokenSpacingProperties.RemovePreceding));
108107
}
109108

109+
bool followedBySpace = token.IsFollowedByWhitespace();
110+
bool lastInLine = token.IsLastInLine();
111+
110112
if (!lastInLine && followedBySpace)
111113
{
112114
// Opening square bracket should {not be followed} by a space.
@@ -121,5 +123,10 @@ private static bool IsPartOfIndexInitializer(SyntaxToken token)
121123
return token.Parent.IsKind(SyntaxKind.BracketedArgumentList)
122124
&& token.Parent.Parent.IsKind(SyntaxKind.ImplicitElementAccess);
123125
}
126+
127+
private static bool IsPartOfListPattern(SyntaxToken token)
128+
{
129+
return token.Parent.IsKind(SyntaxKindEx.ListPattern);
130+
}
124131
}
125132
}

0 commit comments

Comments
 (0)