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;