Skip to content

Commit 91e2fab

Browse files
committed
Fixes #2475
1 parent a17d804 commit 91e2fab

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1008UnitTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,22 @@ class ClassName
20882088
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
20892089
}
20902090

2091+
[Fact]
2092+
[WorkItem(2475, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2475")]
2093+
public async Task TestSingleLineIfStatementAsync()
2094+
{
2095+
var testCode = @"public class TestClass
2096+
{
2097+
public void TestMethod()
2098+
{
2099+
if (true) (true ? 1 : 0).ToString();
2100+
}
2101+
}
2102+
";
2103+
2104+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
2105+
}
2106+
20912107
/// <inheritdoc/>
20922108
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
20932109
{

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,22 @@ class ClassName
907907
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
908908
}
909909

910+
[Fact]
911+
[WorkItem(2475, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2475")]
912+
public async Task TestSingleLineIfStatementAsync()
913+
{
914+
var testCode = @"public class TestClass
915+
{
916+
public void TestMethod()
917+
{
918+
if (true) (true ? 1 : 0).ToString();
919+
}
920+
}
921+
";
922+
923+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
924+
}
925+
910926
/// <inheritdoc/>
911927
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
912928
{

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
8787
switch (nextToken.Kind())
8888
{
8989
case SyntaxKind.OpenParenToken:
90+
// Allow a space between an open and a close paren when:
91+
// - they are part of an if statement
92+
// - they are on the same line
93+
// - the open paren is part of a parenthesized expression
94+
precedesStickyCharacter = !(token.Parent.IsKind(SyntaxKind.IfStatement) && nextToken.Parent.IsKind(SyntaxKind.ParenthesizedExpression) && (token.GetLine() == nextToken.GetLine()));
95+
break;
96+
9097
case SyntaxKind.CloseParenToken:
9198
case SyntaxKind.OpenBracketToken:
9299
case SyntaxKind.CloseBracketToken:

0 commit comments

Comments
 (0)