diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1000CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1000CSharp9UnitTests.cs index 37485b907c..e6c690bb73 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1000CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1000CSharp9UnitTests.cs @@ -10,6 +10,10 @@ namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules using StyleCop.Analyzers.Test.CSharp8.SpacingRules; using Xunit; + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< + StyleCop.Analyzers.SpacingRules.SA1000KeywordsMustBeSpacedCorrectly, + StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; + public class SA1000CSharp9UnitTests : SA1000CSharp8UnitTests { [Fact] @@ -19,5 +23,49 @@ public async Task TestTargetTypedNewAsync() await this.TestKeywordStatementAsync(statementWithoutSpace, DiagnosticResult.EmptyDiagnosticResults, statementWithoutSpace).ConfigureAwait(false); } + + [Fact] + [WorkItem(3508, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3508")] + public async Task TestIsBeforeRelationalPatternAsync() + { + var statementWithoutSpace = "_ = 1 {|#0:is|}>1;"; + var statementWithSpace = "_ = 1 is >1;"; + + var expected = Diagnostic().WithArguments("is", string.Empty, "followed").WithLocation(0); + await this.TestKeywordStatementAsync(statementWithoutSpace, expected, statementWithSpace).ConfigureAwait(false); + } + + [Fact] + [WorkItem(3508, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3508")] + public async Task TestNotBeforeRelationalPatternAsync() + { + var statementWithoutSpace = "_ = 1 is {|#0:not|}>1;"; + var statementWithSpace = "_ = 1 is not >1;"; + + var expected = Diagnostic().WithArguments("not", string.Empty, "followed").WithLocation(0); + await this.TestKeywordStatementAsync(statementWithoutSpace, expected, statementWithSpace).ConfigureAwait(false); + } + + [Fact] + [WorkItem(3508, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3508")] + public async Task TestAndBeforeRelationalPatternAsync() + { + var statementWithoutSpace = "_ = 1 is 1 {|#0:and|}>0;"; + var statementWithSpace = "_ = 1 is 1 and >0;"; + + var expected = Diagnostic().WithArguments("and", string.Empty, "followed").WithLocation(0); + await this.TestKeywordStatementAsync(statementWithoutSpace, expected, statementWithSpace).ConfigureAwait(false); + } + + [Fact] + [WorkItem(3508, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3508")] + public async Task TestOrBeforeRelationalPatternAsync() + { + var statementWithoutSpace = "_ = 1 is 1 {|#0:or|}>1;"; + var statementWithSpace = "_ = 1 is 1 or >1;"; + + var expected = Diagnostic().WithArguments("or", string.Empty, "followed").WithLocation(0); + await this.TestKeywordStatementAsync(statementWithoutSpace, expected, statementWithSpace).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs index 3189a625af..dc524b5985 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs @@ -87,6 +87,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context) { switch (token.Kind()) { + case SyntaxKindEx.AndKeyword: case SyntaxKind.AwaitKeyword: case SyntaxKind.CaseKeyword: case SyntaxKind.CatchKeyword: @@ -98,9 +99,12 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context) case SyntaxKind.IfKeyword: case SyntaxKind.InKeyword: case SyntaxKind.IntoKeyword: + case SyntaxKind.IsKeyword: case SyntaxKind.JoinKeyword: case SyntaxKind.LetKeyword: case SyntaxKind.LockKeyword: + case SyntaxKindEx.NotKeyword: + case SyntaxKindEx.OrKeyword: case SyntaxKind.OrderByKeyword: case SyntaxKind.OutKeyword: case SyntaxKind.RefKeyword: diff --git a/documentation/SA1000.md b/documentation/SA1000.md index ae7b13b2e5..e665509ca1 100644 --- a/documentation/SA1000.md +++ b/documentation/SA1000.md @@ -23,11 +23,11 @@ The spacing around a C# keyword is incorrect. A violation of this rule occurs when the spacing around a keyword is incorrect. -The following C# keywords should always be followed by a single space: `await`, `case`, `catch`, `fixed`, `for`, -`foreach`, `from`, `group`, `if`, `in`, `into`, `join`, `let`, `lock`, `orderby`, `out`, `ref`, `return`, `select`, +The following C# keywords should always be followed by a single space: `and`, `await`, `case`, `catch`, `fixed`, `for`, +`foreach`, `from`, `group`, `if`, `in`, `is`, `into`, `join`, `let`, `lock`, `not`, `orderby`, `or`, `out`, `ref`, `return`, `select`, `switch`, `using`, `var`, `where`, `while`, `yield`. -The following keywords should not be followed by any space: `checked`, `default`, `sizeof`, `typeof`, `unchecked`. +The following keywords should not be followed by any space: `checked`, `default`, `nameof`, `sizeof`, `typeof`, `unchecked`. The `new` and `stackalloc` keywords should always be followed by a space, except in the following cases: