From c7bf1024dc66ac54aeb4cd8792a30cb538626214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Tue, 26 Apr 2022 19:57:59 +0200 Subject: [PATCH] Updated SA1000 to handle checked operator declarations correctly #3478 --- .../SpacingRules/SA1000CSharp11UnitTests.cs | 28 +++++++++++++++++++ .../StyleCop.Analyzers.Test.CSharp11.csproj | 2 +- .../SA1000KeywordsMustBeSpacedCorrectly.cs | 17 ++++++++--- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1000CSharp11UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1000CSharp11UnitTests.cs index 9eb32b207..4e971a44b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1000CSharp11UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1000CSharp11UnitTests.cs @@ -5,9 +5,37 @@ namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules { + using System.Threading; + using System.Threading.Tasks; + using Microsoft.CodeAnalysis.CSharp; using StyleCop.Analyzers.Test.CSharp10.SpacingRules; + using Xunit; + + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< + StyleCop.Analyzers.SpacingRules.SA1000KeywordsMustBeSpacedCorrectly, + StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; public class SA1000CSharp11UnitTests : SA1000CSharp10UnitTests { + [Fact] + public async Task TestCheckedOperatorDeclarationAsync() + { + var testCode = @" +public class MyClass +{ + public static MyClass operator {|#0:checked|}-(MyClass x) => x; + public static MyClass operator -(MyClass x) => x; +}"; + + var fixedCode = @" +public class MyClass +{ + public static MyClass operator checked -(MyClass x) => x; + public static MyClass operator -(MyClass x) => x; +}"; + + var expected = Diagnostic().WithArguments("checked", string.Empty, "followed").WithLocation(0); + await VerifyCSharpFixAsync(LanguageVersion.Preview, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/StyleCop.Analyzers.Test.CSharp11.csproj b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/StyleCop.Analyzers.Test.CSharp11.csproj index 42ff82649..9a25f71eb 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/StyleCop.Analyzers.Test.CSharp11.csproj +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/StyleCop.Analyzers.Test.CSharp11.csproj @@ -17,7 +17,7 @@ - + diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs index 3189a625a..4f896a4a7 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs @@ -115,13 +115,22 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context) case SyntaxKind.CheckedKeyword: case SyntaxKind.UncheckedKeyword: - if (token.GetNextToken().IsKind(SyntaxKind.OpenBraceToken)) + switch (token.Parent.Kind()) { + case SyntaxKind.CheckedStatement: + case SyntaxKind.UncheckedStatement: + case SyntaxKind.OperatorDeclaration: HandleRequiredSpaceToken(ref context, token); - } - else - { + break; + + case SyntaxKind.CheckedExpression: + case SyntaxKind.UncheckedExpression: HandleDisallowedSpaceToken(ref context, token); + break; + + default: + // So far an unknown case, so we have no opinion yet + break; } break;