Skip to content

Commit f6ba016

Browse files
authored
Merge pull request #3084 from pantosha/fix-2885
Make SA1519 work with lock and fixed (fix #2885)
2 parents ad09cf8 + 7dadd9b commit f6ba016

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static IEnumerable<object[]> TestStatements
2929
yield return new[] { "while (i == 0)" };
3030
yield return new[] { "for (var j = 0; j < i; j++)" };
3131
yield return new[] { "foreach (var j in new[] { 1, 2, 3 })" };
32+
yield return new[] { "lock (new object())" };
3233
yield return new[] { "using (default(System.IDisposable))" };
3334
}
3435
}
@@ -363,6 +364,84 @@ public void Bar(int i)
363364
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
364365
}
365366

367+
[Fact]
368+
public async Task TestSingleLineFixedStatementWithoutBracesAsync()
369+
{
370+
var testCode = @"public class C {
371+
unsafe private static void TestMethod()
372+
{
373+
var a = new int[] { 1, 2, 3 };
374+
fixed (int* n = &a[0])
375+
*n = a[1] + a[2];
376+
}
377+
}";
378+
379+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
380+
}
381+
382+
[Fact]
383+
public async Task TestMultiLineFixedStatementWithoutBracesAsync()
384+
{
385+
var testCode = @"public class C {
386+
unsafe private static void TestMethod()
387+
{
388+
var a = new int[] { 1, 2, 3 };
389+
fixed (int* n = &a[0])
390+
*n = a[1]
391+
+ a[2];
392+
}
393+
}";
394+
var fixedCode = @"public class C {
395+
unsafe private static void TestMethod()
396+
{
397+
var a = new int[] { 1, 2, 3 };
398+
fixed (int* n = &a[0])
399+
{
400+
*n = a[1]
401+
+ a[2];
402+
}
403+
}
404+
}";
405+
406+
var expected = Diagnostic().WithLocation(6, 13);
407+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
408+
}
409+
410+
[Fact]
411+
public async Task TestSingleLineFixedStatementWithBracesAsync()
412+
{
413+
var testCode = @"public class C {
414+
unsafe private static void TestMethod()
415+
{
416+
var a = new int[] { 1, 2, 3 };
417+
fixed (int* n = &a[0])
418+
{
419+
*n = a[1] + a[2];
420+
}
421+
}
422+
}";
423+
424+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
425+
}
426+
427+
[Fact]
428+
public async Task TestMultiLineFixedStatementWithBracesAsync()
429+
{
430+
var testCode = @"public class C {
431+
unsafe private static void TestMethod()
432+
{
433+
var a = new int[] { 1, 2, 3 };
434+
fixed (int* n = &a[0])
435+
{
436+
*n = a[1]
437+
+ a[2];
438+
}
439+
}
440+
}";
441+
442+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
443+
}
444+
366445
/// <summary>
367446
/// Verifies that the code fix provider will work properly for a statement.
368447
/// </summary>

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ internal class SA1519BracesMustNotBeOmittedFromMultiLineChildStatement : Diagnos
5252
private static readonly Action<SyntaxNodeAnalysisContext> WhileStatementAction = HandleWhileStatement;
5353
private static readonly Action<SyntaxNodeAnalysisContext> ForStatementAction = HandleForStatement;
5454
private static readonly Action<SyntaxNodeAnalysisContext> ForEachStatementAction = HandleForEachStatement;
55+
private static readonly Action<SyntaxNodeAnalysisContext> LockStatementAction = HandleLockStatement;
56+
private static readonly Action<SyntaxNodeAnalysisContext> FixedStatementAction = HandleFixedStatement;
5557
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> UsingStatementAction = HandleUsingStatement;
5658

5759
/// <inheritdoc/>
@@ -69,6 +71,8 @@ public override void Initialize(AnalysisContext context)
6971
context.RegisterSyntaxNodeAction(WhileStatementAction, SyntaxKind.WhileStatement);
7072
context.RegisterSyntaxNodeAction(ForStatementAction, SyntaxKind.ForStatement);
7173
context.RegisterSyntaxNodeAction(ForEachStatementAction, SyntaxKind.ForEachStatement);
74+
context.RegisterSyntaxNodeAction(LockStatementAction, SyntaxKind.LockStatement);
75+
context.RegisterSyntaxNodeAction(FixedStatementAction, SyntaxKind.FixedStatement);
7276
context.RegisterSyntaxNodeAction(UsingStatementAction, SyntaxKind.UsingStatement);
7377
}
7478

@@ -112,6 +116,18 @@ private static void HandleForEachStatement(SyntaxNodeAnalysisContext context)
112116
CheckChildStatement(context, forEachStatement.Statement);
113117
}
114118

119+
private static void HandleLockStatement(SyntaxNodeAnalysisContext context)
120+
{
121+
var lockStatement = (LockStatementSyntax)context.Node;
122+
CheckChildStatement(context, lockStatement.Statement);
123+
}
124+
125+
private static void HandleFixedStatement(SyntaxNodeAnalysisContext context)
126+
{
127+
var fixedStatement = (FixedStatementSyntax)context.Node;
128+
CheckChildStatement(context, fixedStatement.Statement);
129+
}
130+
115131
private static void HandleUsingStatement(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
116132
{
117133
var usingStatement = (UsingStatementSyntax)context.Node;

0 commit comments

Comments
 (0)