From 7490969803a29c085c12453060744833250a4647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Tue, 26 Apr 2022 21:35:50 +0200 Subject: [PATCH] Updated SA1008 to handle tuples in logical patterns #3476 --- .../SpacingRules/SA1008CSharp9UnitTests.cs | 18 ++++++++++++++++++ .../StyleCop.Analyzers/Lightup/SyntaxKindEx.cs | 3 +++ ...8OpeningParenthesisMustBeSpacedCorrectly.cs | 3 +++ 3 files changed, 24 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1008CSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1008CSharp9UnitTests.cs index d863bf279..038c0d056 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1008CSharp9UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1008CSharp9UnitTests.cs @@ -64,5 +64,23 @@ void Method(int b) FixedCode = fixedCode, }.RunAsync(CancellationToken.None).ConfigureAwait(false); } + + [Fact] + [WorkItem(3476, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3476")] + public async Task TestLogicalTuplePatternAsync() + { + const string testCode = @" +class C +{ + void Method((int, int) c) + { + _ = c is (1, 1) or (2, 2); + _ = c is (1, 1) and (1, 1); + _ = c is not (2, 2); + } +}"; + + await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs index be1f8ec3d..b3964df5f 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs @@ -11,6 +11,9 @@ internal static class SyntaxKindEx { public const SyntaxKind DotDotToken = (SyntaxKind)8222; public const SyntaxKind QuestionQuestionEqualsToken = (SyntaxKind)8284; + public const SyntaxKind OrKeyword = (SyntaxKind)8438; + public const SyntaxKind AndKeyword = (SyntaxKind)8439; + public const SyntaxKind NotKeyword = (SyntaxKind)8440; public const SyntaxKind ManagedKeyword = (SyntaxKind)8445; public const SyntaxKind UnmanagedKeyword = (SyntaxKind)8446; public const SyntaxKind NullableKeyword = (SyntaxKind)8486; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs index 29e41566c..d83d7a847 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs @@ -188,6 +188,9 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt case SyntaxKindEx.PositionalPatternClause: haveLeadingSpace = prevToken.IsKind(SyntaxKind.IsKeyword) + || prevToken.IsKind(SyntaxKindEx.OrKeyword) + || prevToken.IsKind(SyntaxKindEx.AndKeyword) + || prevToken.IsKind(SyntaxKindEx.NotKeyword) || prevToken.IsKind(SyntaxKind.CommaToken); break;