Skip to content

Commit 5540111

Browse files
committed
Update SA1519 to examine using statements
Fixes #2180
1 parent 7fdffd3 commit 5540111

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1519UnitTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static IEnumerable<object[]> TestStatements
2828
yield return new[] { "while (i == 0)" };
2929
yield return new[] { "for (var j = 0; j < i; j++)" };
3030
yield return new[] { "foreach (var j in new[] { 1, 2, 3 })" };
31+
yield return new[] { "using (default(System.IDisposable))" };
3132
}
3233
}
3334

@@ -119,6 +120,44 @@ public void Bar(int i)
119120
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
120121
}
121122

123+
/// <summary>
124+
/// This is a regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2180.
125+
/// </summary>
126+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
127+
[Fact]
128+
public async Task TestMultipleUsingStatementsAsync()
129+
{
130+
var testCode = @"using System;
131+
public class Foo
132+
{
133+
public void Bar(int i)
134+
{
135+
using (default(IDisposable))
136+
using (default(IDisposable))
137+
{
138+
}
139+
}
140+
}";
141+
var fixedCode = @"using System;
142+
public class Foo
143+
{
144+
public void Bar(int i)
145+
{
146+
using (default(IDisposable))
147+
{
148+
using (default(IDisposable))
149+
{
150+
}
151+
}
152+
}
153+
}";
154+
155+
var expected = this.CSharpDiagnostic().WithLocation(7, 9);
156+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
157+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
158+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
159+
}
160+
122161
/// <summary>
123162
/// Verifies that a statement followed by a block with braces will produce no diagnostics results.
124163
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1519BracesMustNotBeOmittedFromMultiLineChildStatement.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ internal class SA1519BracesMustNotBeOmittedFromMultiLineChildStatement : Diagnos
4848
private static readonly Action<SyntaxNodeAnalysisContext> WhileStatementAction = HandleWhileStatement;
4949
private static readonly Action<SyntaxNodeAnalysisContext> ForStatementAction = HandleForStatement;
5050
private static readonly Action<SyntaxNodeAnalysisContext> ForEachStatementAction = HandleForEachStatement;
51+
private static readonly Action<SyntaxNodeAnalysisContext> UsingStatementAction = HandleUsingStatement;
5152

5253
/// <inheritdoc/>
5354
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
@@ -64,6 +65,7 @@ public override void Initialize(AnalysisContext context)
6465
context.RegisterSyntaxNodeAction(WhileStatementAction, SyntaxKind.WhileStatement);
6566
context.RegisterSyntaxNodeAction(ForStatementAction, SyntaxKind.ForStatement);
6667
context.RegisterSyntaxNodeAction(ForEachStatementAction, SyntaxKind.ForEachStatement);
68+
context.RegisterSyntaxNodeAction(UsingStatementAction, SyntaxKind.UsingStatement);
6769
}
6870

6971
private static void HandleIfStatement(SyntaxNodeAnalysisContext context)
@@ -106,6 +108,12 @@ private static void HandleForEachStatement(SyntaxNodeAnalysisContext context)
106108
CheckChildStatement(context, forEachStatement.Statement);
107109
}
108110

111+
private static void HandleUsingStatement(SyntaxNodeAnalysisContext context)
112+
{
113+
var usingStatement = (UsingStatementSyntax)context.Node;
114+
CheckChildStatement(context, usingStatement.Statement);
115+
}
116+
109117
private static void CheckChildStatement(SyntaxNodeAnalysisContext context, StatementSyntax childStatement)
110118
{
111119
if (childStatement is BlockSyntax)

0 commit comments

Comments
 (0)