Skip to content

Commit 6751a39

Browse files
authored
Merge pull request #3418 from sharwell/throw-switch-expression
Allow object initializers and switch expressions in throw statements
2 parents aca684d + 4fdcbcf commit 6751a39

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1513CSharp9UnitTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,37 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp8.LayoutRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.LayoutRules.SA1513ClosingBraceMustBeFollowedByBlankLine,
13+
StyleCop.Analyzers.LayoutRules.SA1513CodeFixProvider>;
714

815
public class SA1513CSharp9UnitTests : SA1513CSharp8UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3410, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3410")]
19+
public async Task TestThrowSwitchExpressionValueAsync()
20+
{
21+
var testCode = @"using System;
22+
23+
public class Foo
24+
{
25+
public void Baz(string arg)
26+
{
27+
throw arg switch
28+
{
29+
"""" => new ArgumentException(),
30+
_ => new Exception()
31+
};
32+
}
33+
}
34+
";
35+
36+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false);
37+
}
1038
}
1139
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ private void AnalyzeCloseBrace(SyntaxToken token)
242242
IsPartOf<EqualsValueClauseSyntax>(token) ||
243243
IsPartOf<AssignmentExpressionSyntax>(token) ||
244244
IsPartOf<ReturnStatementSyntax>(token) ||
245+
IsPartOf<ThrowStatementSyntax>(token) ||
245246
IsPartOf<ObjectCreationExpressionSyntax>(token)))
246247
{
247-
// the close brace is part of a variable initialization statement or a return statement
248+
// the close brace is part of a variable initialization statement or a return/throw statement
248249
return;
249250
}
250251

0 commit comments

Comments
 (0)