From d9364205bbeeed02e5404175e450e92d0ed5d479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 27 Nov 2021 17:42:38 +0100 Subject: [PATCH 1/2] Issue #3386: Updated SA1008 to correctly handle a cast inside a range expression --- .../SpacingRules/SA1008CSharp8UnitTests.cs | 13 ++++++++----- ...SA1008OpeningParenthesisMustBeSpacedCorrectly.cs | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs index 93df4425d..b952eb06f 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1008CSharp8UnitTests.cs @@ -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[.. (finalLen - 1)]; + var test1 = str[.. (finalLen - 1)]; + var test2 = .. (int)finalLen; } } } @@ -52,18 +53,20 @@ 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; } } } "; var expectedResults = new DiagnosticResult[] { - Diagnostic(DescriptorNotPreceded).WithLocation(28, 27), + Diagnostic(DescriptorNotPreceded).WithLocation(28, 32), + Diagnostic(DescriptorNotPreceded).WithLocation(29, 28), }; await VerifyCSharpFixAsync( 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: From 7754e6b38673ef19ec79ab907a8c23c49c09a29b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sun, 28 Nov 2021 09:22:43 +0100 Subject: [PATCH 2/2] Issue #3386: Updated SA1003 to correctly handle a cast inside a range expression --- .../SpacingRules/SA1003CSharp8UnitTests.cs | 61 +++++++++++++++++++ .../SA1003SymbolsMustBeSpacedCorrectly.cs | 4 +- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs index 5c5f983b8..8af4e3eba 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs @@ -3,9 +3,70 @@ 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 StyleCop.Analyzers.Test.Verifiers; + 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] + public async Task TestRangeExpressionAsync() + { + var testCode = SpecialTypeDefinitions.IndexAndRange + @" +namespace TestNamespace +{ + using System; + public class TestClass + { + public void TestMethod() + { + var test1 = .. (int)1; + } + } +} +"; + + var fixedCode = SpecialTypeDefinitions.IndexAndRange + @" +namespace TestNamespace +{ + using System; + public class TestClass + { + public void TestMethod() + { + var test1 = ..(int)1; + } + } +} +"; + var expectedResults = new DiagnosticResult[] + { + Diagnostic(DescriptorNotPrecededByWhitespace).WithLocation(26, 28).WithArguments("(int)"), + }; + + await VerifyCSharpFixAsync( + LanguageVersion.CSharp8, + testCode, + expectedResults, + fixedCode, + 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);