diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs index 5c5f983b8..2ad7f7abc 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs @@ -3,9 +3,67 @@ namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules { + using System.Threading; + using System.Threading.Tasks; + using Microsoft.CodeAnalysis.CSharp; + using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp7.SpacingRules; + using Xunit; + + using static StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly; + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< + StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly, + StyleCop.Analyzers.SpacingRules.SA1003CodeFixProvider>; public class SA1003CSharp8UnitTests : SA1003CSharp7UnitTests { + /// + /// Verifies that spacing around a range expression double dots isn't required. + /// + /// + /// Double dots of range expressions already provide enough spacing for readability so there is no + /// need to surround the range expression with spaces. + /// + /// A representing the asynchronous unit test. + [Fact] + [WorkItem(3386, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3386")] + public async Task TestRangeExpressionAsync() + { + var testCode = @" +namespace TestNamespace +{ + using System; + public class TestClass + { + public void TestMethod() + { + var test1 = .. {|#0:(|}int)1; + } + } +} +"; + + var fixedCode = @" +namespace TestNamespace +{ + using System; + public class TestClass + { + public void TestMethod() + { + var test1 = ..(int)1; + } + } +} +"; + + await new CSharpTest(LanguageVersion.CSharp8) + { + ReferenceAssemblies = ReferenceAssemblies.NetCore.NetCoreApp31, + TestCode = testCode, + ExpectedDiagnostics = { Diagnostic(DescriptorNotPrecededByWhitespace).WithLocation(0).WithArguments("(int)") }, + FixedCode = fixedCode, + }.RunAsync(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 36b16dd72..273a7de83 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs @@ -8,7 +8,6 @@ namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp7.SpacingRules; - using StyleCop.Analyzers.Test.Verifiers; using Xunit; using static StyleCop.Analyzers.SpacingRules.SA1008OpeningParenthesisMustBeSpacedCorrectly; @@ -28,6 +27,7 @@ public class SA1008CSharp8UnitTests : SA1008CSharp7UnitTests /// /// A representing the asynchronous unit test. [Fact] + [WorkItem(3386, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3386")] public async Task TestAfterRangeExpressionAsync() { var testCode = @" @@ -36,11 +36,12 @@ namespace TestNamespace using System; public class TestClass { - public string TestMethod() + public void TestMethod() { string str = ""test""; int finalLen = 4; - return str[.. {|#0:(|}finalLen - 1)]; + var test1 = str[.. {|#0:(|}finalLen - 1)]; + var test2 = .. {|#1:(|}int)finalLen; } } } @@ -52,11 +53,12 @@ namespace TestNamespace using System; public class TestClass { - public string TestMethod() + public void TestMethod() { string str = ""test""; int finalLen = 4; - return str[..(finalLen - 1)]; + var test1 = str[..(finalLen - 1)]; + var test2 = ..(int)finalLen; } } } @@ -66,7 +68,11 @@ public string TestMethod() { ReferenceAssemblies = ReferenceAssemblies.NetCore.NetCoreApp31, TestCode = testCode, - ExpectedDiagnostics = { Diagnostic(DescriptorNotPreceded).WithLocation(0) }, + ExpectedDiagnostics = + { + Diagnostic(DescriptorNotPreceded).WithLocation(0), + Diagnostic(DescriptorNotPreceded).WithLocation(1), + }, FixedCode = fixedCode, }.RunAsync(CancellationToken.None).ConfigureAwait(false); } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1003SymbolsMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1003SymbolsMustBeSpacedCorrectly.cs index 150a69216..f4271be46 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1003SymbolsMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1003SymbolsMustBeSpacedCorrectly.cs @@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.SpacingRules using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using StyleCop.Analyzers.Helpers; + using StyleCop.Analyzers.Lightup; /// /// The spacing around an operator symbol is incorrect, within a C# code file. @@ -344,7 +345,8 @@ private static void HandleCastExpression(SyntaxNodeAnalysisContext context) && !(castExpression.Parent is CastExpressionSyntax) && !precedingToken.IsKind(SyntaxKind.OpenParenToken) && !precedingToken.IsKind(SyntaxKind.OpenBracketToken) - && !(precedingToken.IsKind(SyntaxKind.OpenBraceToken) && (precedingToken.Parent is InterpolationSyntax)); + && !(precedingToken.IsKind(SyntaxKind.OpenBraceToken) && (precedingToken.Parent is InterpolationSyntax)) + && !precedingToken.IsKind(SyntaxKindEx.DotDotToken); var tokenString = castExpression.OpenParenToken.ToString() + castExpression.Type.ToString() + castExpression.CloseParenToken.ToString(); CheckToken(context, castExpression.OpenParenToken, mustHaveLeadingWhitespace, false, false, tokenString); diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs index 6f8220da2..90ade9103 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs @@ -231,8 +231,9 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt startOfIndexer = prevToken.IsKind(SyntaxKind.OpenBracketToken); var consecutiveCast = prevToken.IsKind(SyntaxKind.CloseParenToken) && prevToken.Parent.IsKind(SyntaxKind.CastExpression); var partOfInterpolation = prevToken.IsKind(SyntaxKind.OpenBraceToken) && prevToken.Parent.IsKind(SyntaxKind.Interpolation); + var partOfRange = prevToken.IsKind(SyntaxKindEx.DotDotToken); - haveLeadingSpace = !partOfUnaryExpression && !startOfIndexer && !consecutiveCast && !partOfInterpolation; + haveLeadingSpace = !partOfUnaryExpression && !startOfIndexer && !consecutiveCast && !partOfInterpolation && !partOfRange; break; case SyntaxKind.ParameterList: