Skip to content

Commit f4e85a2

Browse files
committed
Added suggested unit test and fixed code
1 parent ee3d7f5 commit f4e85a2

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1119CodeFixProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ private static SyntaxNode GetReplacement(ParenthesizedExpressionSyntax oldNode)
7474
if (!leadingTrivia.Any())
7575
{
7676
var previousToken = oldNode.OpenParenToken.GetPreviousToken();
77-
if (!previousToken.IsKind(SyntaxKind.OpenParenToken) && (TriviaHelper.IndexOfTrailingWhitespace(previousToken.TrailingTrivia) == -1))
77+
if (!(previousToken.IsKind(SyntaxKind.OpenParenToken) || previousToken.IsKind(SyntaxKind.CloseParenToken))
78+
&& (TriviaHelper.IndexOfTrailingWhitespace(previousToken.TrailingTrivia) == -1))
7879
{
7980
leadingTrivia = SyntaxFactory.TriviaList(SyntaxFactory.Space);
8081
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,45 @@ public string TestMethod(int n, object a, object b)
4242
}
4343

4444
/// <summary>
45-
/// Verifies that a swith expression with unnecessary parenthesis is handled correcly.
45+
/// Verifies that a type cast followed by a switch expression with unnecessary parenthesis is handled correctly.
46+
/// </summary>
47+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
48+
[Fact]
49+
[WorkItem(3171, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3171")]
50+
public async Task TestTypeCastFollowedBySwitchExpressionWithUnnecessaryParenthesisIsHandledCorrectlyAsync()
51+
{
52+
const string testCode = @"
53+
public class Foo
54+
{
55+
public string TestMethod(int n, object a, object b)
56+
{
57+
return (string)((n switch { 1 => a, 2 => b }));
58+
}
59+
}
60+
";
61+
62+
const string fixedCode = @"
63+
public class Foo
64+
{
65+
public string TestMethod(int n, object a, object b)
66+
{
67+
return (string)(n switch { 1 => a, 2 => b });
68+
}
69+
}
70+
";
71+
72+
DiagnosticResult[] expected =
73+
{
74+
Diagnostic(DiagnosticId).WithSpan(6, 24, 6, 55),
75+
Diagnostic(ParenthesesDiagnosticId).WithLocation(6, 24),
76+
Diagnostic(ParenthesesDiagnosticId).WithLocation(6, 54),
77+
};
78+
79+
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
80+
}
81+
82+
/// <summary>
83+
/// Verifies that a switch expression with unnecessary parenthesis is handled correcly.
4684
/// </summary>
4785
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
4886
[Fact]

StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ private static bool IsSwitchExpressionPrecededByTypeCast(ParenthesizedExpression
210210
}
211211

212212
var previousToken = node.OpenParenToken.GetPreviousToken();
213+
214+
while (previousToken.IsKind(SyntaxKind.OpenParenToken) && previousToken.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
215+
{
216+
previousToken = previousToken.GetPreviousToken();
217+
}
218+
213219
return previousToken.IsKind(SyntaxKind.CloseParenToken) && previousToken.Parent.IsKind(SyntaxKind.CastExpression);
214220
}
215221

0 commit comments

Comments
 (0)