Skip to content

Commit 7754e6b

Browse files
Issue #3386: Updated SA1003 to correctly handle a cast inside a range expression
1 parent d936420 commit 7754e6b

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,70 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
11+
using StyleCop.Analyzers.Test.Verifiers;
12+
using Xunit;
13+
14+
using static StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly;
15+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
16+
StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly,
17+
StyleCop.Analyzers.SpacingRules.SA1003CodeFixProvider>;
718

819
public class SA1003CSharp8UnitTests : SA1003CSharp7UnitTests
920
{
21+
/// <summary>
22+
/// Verifies that spacing around a range expression double dots isn't required.
23+
/// </summary>
24+
/// <remarks>
25+
/// <para>Double dots of range expressions already provide enough spacing for readability so there is no
26+
/// need to surround the range expression with spaces.</para>
27+
/// </remarks>
28+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
29+
[Fact]
30+
public async Task TestRangeExpressionAsync()
31+
{
32+
var testCode = SpecialTypeDefinitions.IndexAndRange + @"
33+
namespace TestNamespace
34+
{
35+
using System;
36+
public class TestClass
37+
{
38+
public void TestMethod()
39+
{
40+
var test1 = .. (int)1;
41+
}
42+
}
43+
}
44+
";
45+
46+
var fixedCode = SpecialTypeDefinitions.IndexAndRange + @"
47+
namespace TestNamespace
48+
{
49+
using System;
50+
public class TestClass
51+
{
52+
public void TestMethod()
53+
{
54+
var test1 = ..(int)1;
55+
}
56+
}
57+
}
58+
";
59+
var expectedResults = new DiagnosticResult[]
60+
{
61+
Diagnostic(DescriptorNotPrecededByWhitespace).WithLocation(26, 28).WithArguments("(int)"),
62+
};
63+
64+
await VerifyCSharpFixAsync(
65+
LanguageVersion.CSharp8,
66+
testCode,
67+
expectedResults,
68+
fixedCode,
69+
CancellationToken.None).ConfigureAwait(false);
70+
}
1071
}
1172
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1003SymbolsMustBeSpacedCorrectly.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.SpacingRules
1010
using Microsoft.CodeAnalysis.CSharp.Syntax;
1111
using Microsoft.CodeAnalysis.Diagnostics;
1212
using StyleCop.Analyzers.Helpers;
13+
using StyleCop.Analyzers.Lightup;
1314

1415
/// <summary>
1516
/// The spacing around an operator symbol is incorrect, within a C# code file.
@@ -344,7 +345,8 @@ private static void HandleCastExpression(SyntaxNodeAnalysisContext context)
344345
&& !(castExpression.Parent is CastExpressionSyntax)
345346
&& !precedingToken.IsKind(SyntaxKind.OpenParenToken)
346347
&& !precedingToken.IsKind(SyntaxKind.OpenBracketToken)
347-
&& !(precedingToken.IsKind(SyntaxKind.OpenBraceToken) && (precedingToken.Parent is InterpolationSyntax));
348+
&& !(precedingToken.IsKind(SyntaxKind.OpenBraceToken) && (precedingToken.Parent is InterpolationSyntax))
349+
&& !precedingToken.IsKind(SyntaxKindEx.DotDotToken);
348350

349351
var tokenString = castExpression.OpenParenToken.ToString() + castExpression.Type.ToString() + castExpression.CloseParenToken.ToString();
350352
CheckToken(context, castExpression.OpenParenToken, mustHaveLeadingWhitespace, false, false, tokenString);

0 commit comments

Comments
 (0)