Skip to content

Commit

Permalink
Merge pull request #3750 from bjornhellander/feature/sa1009-if-one-liner
Browse files Browse the repository at this point in the history
Update SA1009 to require a space after the closing parenthesis if it is followed by ++ or -- from a prefix unary expression
  • Loading branch information
sharwell committed Dec 18, 2023
2 parents be49652 + 63700b5 commit 4495405
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 4495405

Please sign in to comment.