From 2d7e105b5016ef843e2a4b2f6b72a514c7014719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Thu, 17 Oct 2024 20:25:10 +0200 Subject: [PATCH] Update SA1008 to require a space before the opening parenthesis if it's the left operand of a range expression. Also add test for SA1008 in collection expression. #3894 --- .../SpacingRules/SA1008CSharp12UnitTests.cs | 44 +++++++++++++++++++ .../SpacingRules/SA1008CSharp8UnitTests.cs | 43 ++++++++++++++++++ ...OpeningParenthesisMustBeSpacedCorrectly.cs | 2 +- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1008CSharp12UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1008CSharp12UnitTests.cs index f0544bc37..24cb971c4 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1008CSharp12UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1008CSharp12UnitTests.cs @@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.Test.CSharp12.SpacingRules { using System.Threading; using System.Threading.Tasks; + using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp11.SpacingRules; using Xunit; @@ -28,5 +29,48 @@ public async Task TestTupleUsingAliasAsync() var expected = Diagnostic(DescriptorPreceded).WithLocation(0); await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } + + [Fact] + [WorkItem(3894, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3894")] + public async Task TestCollectionExpressionAsync() + { + var testCode = @" +namespace TestNamespace +{ + public class TestClass + { + public void TestMethod() + { + int[] x = [ {|#0:(|} 0 + 0)]; + } + } +} +"; + + var fixedCode = @" +namespace TestNamespace +{ + public class TestClass + { + public void TestMethod() + { + int[] x = [(0 + 0)]; + } + } +} +"; + + DiagnosticResult[] expectedResults = + { + Diagnostic(DescriptorNotPreceded).WithLocation(0), + Diagnostic(DescriptorNotFollowed).WithLocation(0), + }; + + await VerifyCSharpFixAsync( + testCode, + expectedResults, + fixedCode, + CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs index cbdc8fc61..e6b8c6e74 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs @@ -194,5 +194,48 @@ await VerifyCSharpFixAsync( fixedCode, CancellationToken.None).ConfigureAwait(false); } + + [Fact] + [WorkItem(3894, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3894")] + public async Task TestLeftOperandInRangeExpressionAsync() + { + var testCode = @" +namespace TestNamespace +{ + public class TestClass + { + public void TestMethod() + { + var x ={|#0:(|} 0 + 0)..1; + } + } +} +"; + + var fixedCode = @" +namespace TestNamespace +{ + public class TestClass + { + public void TestMethod() + { + var x = (0 + 0)..1; + } + } +} +"; + + DiagnosticResult[] expectedResults = + { + Diagnostic(DescriptorPreceded).WithLocation(0), + Diagnostic(DescriptorNotFollowed).WithLocation(0), + }; + + await VerifyCSharpFixAsync( + testCode, + expectedResults, + fixedCode, + CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs index 4f3a67fe2..929ab023b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs @@ -223,7 +223,7 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt case SyntaxKind.ParenthesizedExpression: case SyntaxKindEx.TupleExpression: if (prevToken.Parent.IsKind(SyntaxKind.Interpolation) - || token.Parent.Parent.IsKind(SyntaxKindEx.RangeExpression)) + || (token.Parent.Parent.IsKind(SyntaxKindEx.RangeExpression) && ((RangeExpressionSyntaxWrapper)token.Parent.Parent).RightOperand == token.Parent)) { haveLeadingSpace = false; break;