Skip to content

Commit 3ea6146

Browse files
committed
Update SA1106 for asynchronous streams
1 parent be0c6a9 commit 3ea6146

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1106CodeFixProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private static async Task<Document> RemoveEmptyStatementAsync(Document document,
7777
case SyntaxKind.IfStatement:
7878
case SyntaxKind.ElseClause:
7979
case SyntaxKind.ForStatement:
80+
case SyntaxKind.ForEachStatement:
8081
case SyntaxKind.WhileStatement:
8182
case SyntaxKind.DoStatement:
8283
// these cases are always replaced with an empty block

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/ReadabilityRules/SA1106CSharp8UnitTests.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp8.ReadabilityRules
75
{
86
using System.Threading;
@@ -32,5 +30,39 @@ public void Method()
3230

3331
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3432
}
33+
34+
[Fact]
35+
[WorkItem(3007, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3007")]
36+
public async Task TestAwaitForEachEmptyStatementAsync()
37+
{
38+
var testCode = @"
39+
using System.Collections.Generic;
40+
using System.Threading.Tasks;
41+
42+
public class TestClass
43+
{
44+
public async Task TestAsync(IAsyncEnumerable<int> values)
45+
{
46+
await foreach (var value in values){|#0:;|}
47+
}
48+
}
49+
";
50+
var fixedCode = @"
51+
using System.Collections.Generic;
52+
using System.Threading.Tasks;
53+
54+
public class TestClass
55+
{
56+
public async Task TestAsync(IAsyncEnumerable<int> values)
57+
{
58+
await foreach (var value in values)
59+
{
60+
}
61+
}
62+
}
63+
";
64+
65+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0), fixedCode, CancellationToken.None).ConfigureAwait(false);
66+
}
3567
}
3668
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1106UnitTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class SA1106UnitTests
2020
[InlineData("if (true)")]
2121
[InlineData("if (true) { } else")]
2222
[InlineData("for (int i = 0; i < 10; i++)")]
23+
[InlineData("foreach (int i in new int[] { 0 })")]
2324
[InlineData("while (true)")]
2425
public async Task TestEmptyStatementAsBlockAsync(string controlFlowConstruct)
2526
{

0 commit comments

Comments
 (0)