Skip to content

Commit 477eb3e

Browse files
authored
Merge pull request #3585 from bjornhellander/feature/is_not_or_and
Update SA1000 to trigger after keywords is, or, and, not
2 parents 4810fdd + c6b9614 commit 477eb3e

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1000CSharp9UnitTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
1010
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
1111
using Xunit;
1212

13+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
14+
StyleCop.Analyzers.SpacingRules.SA1000KeywordsMustBeSpacedCorrectly,
15+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
16+
1317
public class SA1000CSharp9UnitTests : SA1000CSharp8UnitTests
1418
{
1519
[Fact]
@@ -19,5 +23,49 @@ public async Task TestTargetTypedNewAsync()
1923

2024
await this.TestKeywordStatementAsync(statementWithoutSpace, DiagnosticResult.EmptyDiagnosticResults, statementWithoutSpace).ConfigureAwait(false);
2125
}
26+
27+
[Fact]
28+
[WorkItem(3508, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3508")]
29+
public async Task TestIsBeforeRelationalPatternAsync()
30+
{
31+
var statementWithoutSpace = "_ = 1 {|#0:is|}>1;";
32+
var statementWithSpace = "_ = 1 is >1;";
33+
34+
var expected = Diagnostic().WithArguments("is", string.Empty, "followed").WithLocation(0);
35+
await this.TestKeywordStatementAsync(statementWithoutSpace, expected, statementWithSpace).ConfigureAwait(false);
36+
}
37+
38+
[Fact]
39+
[WorkItem(3508, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3508")]
40+
public async Task TestNotBeforeRelationalPatternAsync()
41+
{
42+
var statementWithoutSpace = "_ = 1 is {|#0:not|}>1;";
43+
var statementWithSpace = "_ = 1 is not >1;";
44+
45+
var expected = Diagnostic().WithArguments("not", string.Empty, "followed").WithLocation(0);
46+
await this.TestKeywordStatementAsync(statementWithoutSpace, expected, statementWithSpace).ConfigureAwait(false);
47+
}
48+
49+
[Fact]
50+
[WorkItem(3508, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3508")]
51+
public async Task TestAndBeforeRelationalPatternAsync()
52+
{
53+
var statementWithoutSpace = "_ = 1 is 1 {|#0:and|}>0;";
54+
var statementWithSpace = "_ = 1 is 1 and >0;";
55+
56+
var expected = Diagnostic().WithArguments("and", string.Empty, "followed").WithLocation(0);
57+
await this.TestKeywordStatementAsync(statementWithoutSpace, expected, statementWithSpace).ConfigureAwait(false);
58+
}
59+
60+
[Fact]
61+
[WorkItem(3508, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3508")]
62+
public async Task TestOrBeforeRelationalPatternAsync()
63+
{
64+
var statementWithoutSpace = "_ = 1 is 1 {|#0:or|}>1;";
65+
var statementWithSpace = "_ = 1 is 1 or >1;";
66+
67+
var expected = Diagnostic().WithArguments("or", string.Empty, "followed").WithLocation(0);
68+
await this.TestKeywordStatementAsync(statementWithoutSpace, expected, statementWithSpace).ConfigureAwait(false);
69+
}
2270
}
2371
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
8787
{
8888
switch (token.Kind())
8989
{
90+
case SyntaxKindEx.AndKeyword:
9091
case SyntaxKind.AwaitKeyword:
9192
case SyntaxKind.CaseKeyword:
9293
case SyntaxKind.CatchKeyword:
@@ -98,9 +99,12 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
9899
case SyntaxKind.IfKeyword:
99100
case SyntaxKind.InKeyword:
100101
case SyntaxKind.IntoKeyword:
102+
case SyntaxKind.IsKeyword:
101103
case SyntaxKind.JoinKeyword:
102104
case SyntaxKind.LetKeyword:
103105
case SyntaxKind.LockKeyword:
106+
case SyntaxKindEx.NotKeyword:
107+
case SyntaxKindEx.OrKeyword:
104108
case SyntaxKind.OrderByKeyword:
105109
case SyntaxKind.OutKeyword:
106110
case SyntaxKind.RefKeyword:

documentation/SA1000.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ The spacing around a C# keyword is incorrect.
2323

2424
A violation of this rule occurs when the spacing around a keyword is incorrect.
2525

26-
The following C# keywords should always be followed by a single space: `await`, `case`, `catch`, `fixed`, `for`,
27-
`foreach`, `from`, `group`, `if`, `in`, `into`, `join`, `let`, `lock`, `orderby`, `out`, `ref`, `return`, `select`,
26+
The following C# keywords should always be followed by a single space: `and`, `await`, `case`, `catch`, `fixed`, `for`,
27+
`foreach`, `from`, `group`, `if`, `in`, `is`, `into`, `join`, `let`, `lock`, `not`, `orderby`, `or`, `out`, `ref`, `return`, `select`,
2828
`switch`, `using`, `var`, `where`, `while`, `yield`.
2929

30-
The following keywords should not be followed by any space: `checked`, `default`, `sizeof`, `typeof`, `unchecked`.
30+
The following keywords should not be followed by any space: `checked`, `default`, `nameof`, `sizeof`, `typeof`, `unchecked`.
3131

3232
The `new` and `stackalloc` keywords should always be followed by a space, except in the following cases:
3333

0 commit comments

Comments
 (0)