Skip to content

Commit 63700b5

Browse files
Update SA1009 to require a space after the closing parenthesis if it is followed by ++ or -- from a prefix unary expression
#3731
1 parent be49652 commit 63700b5

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1009UnitTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,49 @@ await Task.Delay(1000)
985985
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
986986
}
987987

988+
[Theory]
989+
[InlineData("if (true)")]
990+
[InlineData("while (true)")]
991+
[InlineData("for (var i = 0; i < 10; i++)")]
992+
[InlineData("foreach (var i in a)")]
993+
[WorkItem(3731, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3731")]
994+
public async Task TestControlStatementWithBodyOnSameLineAsync(string stmt)
995+
{
996+
var testCode = $@"
997+
public class TestClass
998+
{{
999+
public async void TestMethod(int x, int[] a)
1000+
{{
1001+
{stmt}++x;
1002+
{stmt}--x;
1003+
{stmt}x++;
1004+
{stmt}{{ x++; }}
1005+
}}
1006+
}}";
1007+
1008+
var fixedCode = $@"
1009+
public class TestClass
1010+
{{
1011+
public async void TestMethod(int x, int[] a)
1012+
{{
1013+
{stmt} ++x;
1014+
{stmt} --x;
1015+
{stmt} x++;
1016+
{stmt} {{ x++; }}
1017+
}}
1018+
}}";
1019+
1020+
var expected = new[]
1021+
{
1022+
Diagnostic(DescriptorFollowed).WithLocation(6, 8 + stmt.Length),
1023+
Diagnostic(DescriptorFollowed).WithLocation(7, 8 + stmt.Length),
1024+
Diagnostic(DescriptorFollowed).WithLocation(8, 8 + stmt.Length),
1025+
Diagnostic(DescriptorFollowed).WithLocation(9, 8 + stmt.Length),
1026+
};
1027+
1028+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
1029+
}
1030+
9881031
private async Task TestWhitespaceInStatementOrDeclAsync(string originalStatement, string fixedStatement, params DiagnosticResult[] expected)
9891032
{
9901033
string template = @"namespace Foo

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1009ClosingParenthesisMustBeSpacedCorrectly.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
181181

182182
case SyntaxKind.PlusPlusToken:
183183
case SyntaxKind.MinusMinusToken:
184-
precedesStickyCharacter = true;
184+
precedesStickyCharacter =
185+
!nextToken.Parent.IsKind(SyntaxKind.PreIncrementExpression)
186+
&& !nextToken.Parent.IsKind(SyntaxKind.PreDecrementExpression);
185187
suppressFollowingSpaceError = false;
186188
break;
187189

0 commit comments

Comments
 (0)