Skip to content

Commit 01816d6

Browse files
authored
Merge pull request #2483 from vweijsters/fix-2475
Fixes Conflict between SA1008 and SA1009 for single-line if
2 parents a17d804 + 6db7315 commit 01816d6

5 files changed

Lines changed: 75 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1008CSharp7UnitTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,5 +946,21 @@ public void TestMethod()
946946
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
947947
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
948948
}
949+
950+
[Fact]
951+
[WorkItem(2475, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2475")]
952+
public async Task TestSingleLineIfStatementWithTupleExpressionAsync()
953+
{
954+
var testCode = @"public class TestClass
955+
{
956+
public void TestMethod()
957+
{
958+
if (true) (1, 2).ToString();
959+
}
960+
}
961+
";
962+
963+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
964+
}
949965
}
950966
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1009CSharp7UnitTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,5 +1025,21 @@ public void TestMethod()
10251025
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
10261026
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
10271027
}
1028+
1029+
[Fact]
1030+
[WorkItem(2475, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2475")]
1031+
public async Task TestSingleLineIfStatementWithTupleExpressionAsync()
1032+
{
1033+
var testCode = @"public class TestClass
1034+
{
1035+
public void TestMethod()
1036+
{
1037+
if (true) (1, 2).ToString();
1038+
}
1039+
}
1040+
";
1041+
1042+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
1043+
}
10281044
}
10291045
}

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: 11 additions & 0 deletions
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
/// A closing parenthesis within a C# statement is not spaced correctly.
@@ -87,6 +88,16 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
8788
switch (nextToken.Kind())
8889
{
8990
case SyntaxKind.OpenParenToken:
91+
// Allow a space between an open and a close paren when:
92+
// - they are part of an if statement
93+
// - they are on the same line
94+
// - the open paren is part of a parenthesized expression or a tuple expression.
95+
precedesStickyCharacter =
96+
!(token.Parent.IsKind(SyntaxKind.IfStatement)
97+
&& (token.GetLine() == nextToken.GetLine())
98+
&& (nextToken.Parent.IsKind(SyntaxKind.ParenthesizedExpression) || nextToken.Parent.IsKind(SyntaxKindEx.TupleExpression)));
99+
break;
100+
90101
case SyntaxKind.CloseParenToken:
91102
case SyntaxKind.OpenBracketToken:
92103
case SyntaxKind.CloseBracketToken:

0 commit comments

Comments
 (0)