Skip to content

Commit 73f64f7

Browse files
authored
Merge pull request #3733 from bjornhellander/feature/sa1119-switch-expr-call
Update SA1119 to allow parenthesized switch expressions followed by an invocation
2 parents 081e2d0 + 8dd785a commit 73f64f7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,25 @@ public unsafe string TestMethod(int n, byte* a, byte* b)
219219
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
220220
}
221221

222+
[Fact]
223+
[WorkItem(3730, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3730")]
224+
public async Task TestSwitchExpressionFollowedByInvocationAsync()
225+
{
226+
string testCode = @"
227+
using System;
228+
229+
public class Foo
230+
{
231+
public string TestMethod(int n, Func<string> a, Func<string> b)
232+
{
233+
return (n switch { 1 => a, 2 => b })();
234+
}
235+
}
236+
";
237+
238+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
239+
}
240+
222241
[Fact]
223242
public async Task TestStackAllocExpressionInExpressionAsync()
224243
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ private static bool IsSwitchOrWithExpressionWithRequiredParentheses(Parenthesize
239239
MemberAccessExpressionSyntax memberAccessExpression => memberAccessExpression.Expression == outerExpression,
240240
ConditionalAccessExpressionSyntax conditionalAccessExpression => conditionalAccessExpression.Expression == outerExpression,
241241
ElementAccessExpressionSyntax elementAccessExpression => elementAccessExpression.Expression == outerExpression,
242+
InvocationExpressionSyntax invocationExpression => invocationExpression.Expression == outerExpression,
242243
_ => false,
243244
};
244245
}

0 commit comments

Comments
 (0)