From 94243e6e464d97d479896b274ed31db46b802c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 15 Apr 2023 07:33:20 +0200 Subject: [PATCH] Update SA1010 to not trigger on list patterns --- .../SpacingRules/SA1010CSharp11UnitTests.cs | 39 +++++++++++++++++++ ...ningSquareBracketsMustBeSpacedCorrectly.cs | 15 +++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1010CSharp11UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1010CSharp11UnitTests.cs index c4149c234..58fb98289 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1010CSharp11UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1010CSharp11UnitTests.cs @@ -3,9 +3,48 @@ namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules { + using System.Threading; + using System.Threading.Tasks; using StyleCop.Analyzers.Test.CSharp10.SpacingRules; + using StyleCop.Analyzers.Test.Verifiers; + using Xunit; + + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< + StyleCop.Analyzers.SpacingRules.SA1010OpeningSquareBracketsMustBeSpacedCorrectly, + StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; public class SA1010CSharp11UnitTests : SA1010CSharp10UnitTests { + [Theory] + [InlineData("x is [1]")] + [InlineData("x is not [1]")] + [InlineData("x is ([1] or [2])")] + [InlineData("x is ([1] or not [2])")] + [InlineData("x is ([1] and [1])")] + [InlineData("x is ([1] and not [2])")] + [WorkItem(3503, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3503")] + public async Task TestListPatternAsync(string condition) + { + var testCode = $@" +using System.Collections.Generic; + +namespace TestNamespace +{{ + public class TestClass + {{ + public void TestMethod(List x) + {{ + _ = {condition}; + }} + }} +}} +"; + + await new CSharpTest() + { + ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50, + TestCode = testCode, + }.RunAsync(CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs index f9d9f6cac..aa5dacfcf 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.SpacingRules using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; using StyleCop.Analyzers.Helpers; + using StyleCop.Analyzers.Lightup; /// /// An opening square bracket within a C# statement is not spaced correctly. @@ -98,15 +99,16 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy } } - bool followedBySpace = token.IsFollowedByWhitespace(); - bool lastInLine = token.IsLastInLine(); - - if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem && !IsPartOfIndexInitializer(token)) + if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem && + !IsPartOfIndexInitializer(token) && !IsPartOfListPattern(token)) { // Opening square bracket should {not be preceded} by a space. context.ReportDiagnostic(Diagnostic.Create(DescriptorNotPreceded, token.GetLocation(), TokenSpacingProperties.RemovePreceding)); } + bool followedBySpace = token.IsFollowedByWhitespace(); + bool lastInLine = token.IsLastInLine(); + if (!lastInLine && followedBySpace) { // Opening square bracket should {not be followed} by a space. @@ -121,5 +123,10 @@ private static bool IsPartOfIndexInitializer(SyntaxToken token) return token.Parent.IsKind(SyntaxKind.BracketedArgumentList) && token.Parent.Parent.IsKind(SyntaxKind.ImplicitElementAccess); } + + private static bool IsPartOfListPattern(SyntaxToken token) + { + return token.Parent.IsKind(SyntaxKindEx.ListPattern); + } } }