Skip to content

Commit

Permalink
Update SA1009 to require a space after the closing parenthesis if is …
Browse files Browse the repository at this point in the history
…is followed by ++ or -- from a prefix unary expression

#3731
  • Loading branch information
bjornhellander committed Dec 15, 2023
1 parent be49652 commit cd288a2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,49 @@ await Task.Delay(1000)
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[InlineData("if (true)")]
[InlineData("while (true)")]
[InlineData("for (var i = 0; i < 10; i++)")]
[InlineData("foreach (var i in a)")]
[WorkItem(3731, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3731")]
public async Task TestControlStatementWithBodyOnSameLineAsync(string stmt)
{
var testCode = $@"
public class TestClass
{{
public async void TestMethod(int x, int[] a)
{{
{stmt}++x;
{stmt}--x;
{stmt}x++;
{stmt}{{ x++; }}
}}
}}";

var fixedCode = $@"
public class TestClass
{{
public async void TestMethod(int x, int[] a)
{{
{stmt} ++x;
{stmt} --x;
{stmt} x++;
{stmt} {{ x++; }}
}}
}}";

var expected = new[]
{
Diagnostic(DescriptorFollowed).WithLocation(6, 8 + stmt.Length),
Diagnostic(DescriptorFollowed).WithLocation(7, 8 + stmt.Length),
Diagnostic(DescriptorFollowed).WithLocation(8, 8 + stmt.Length),
Diagnostic(DescriptorFollowed).WithLocation(9, 8 + stmt.Length),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

private async Task TestWhitespaceInStatementOrDeclAsync(string originalStatement, string fixedStatement, params DiagnosticResult[] expected)
{
string template = @"namespace Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn

case SyntaxKind.PlusPlusToken:
case SyntaxKind.MinusMinusToken:
precedesStickyCharacter = true;
precedesStickyCharacter =
!nextToken.Parent.IsKind(SyntaxKind.PreIncrementExpression)
&& !nextToken.Parent.IsKind(SyntaxKind.PreDecrementExpression);
suppressFollowingSpaceError = false;
break;

Expand Down

0 comments on commit cd288a2

Please sign in to comment.