Skip to content

Commit bb808af

Browse files
committed
Only report a hidden diagnostic for cases where SA1501 overlaps with SA1519
1 parent 2175a3c commit bb808af

3 files changed

Lines changed: 209 additions & 152 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
3939
{
4040
foreach (Diagnostic diagnostic in context.Diagnostics)
4141
{
42+
if (diagnostic.Properties.GetValueOrDefault(SA1501StatementMustNotBeOnASingleLine.SuppressCodeFixKey) == SA1501StatementMustNotBeOnASingleLine.SuppressCodeFixValue)
43+
{
44+
continue;
45+
}
46+
4247
context.RegisterCodeFix(
4348
CodeAction.Create(
4449
LayoutResources.SA1501CodeFix,

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1501UnitTests.cs

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace StyleCop.Analyzers.Test.LayoutRules
55
{
66
using System.Collections.Generic;
7-
using System.Collections.Immutable;
87
using System.Threading;
98
using System.Threading.Tasks;
109
using Microsoft.CodeAnalysis;
@@ -646,7 +645,66 @@ public void Bar(int i)
646645
}
647646
}";
648647

649-
var fixedCode = @"using System.Diagnostics;
648+
var incrementalFixedCode = @"using System.Diagnostics;
649+
public class TypeName
650+
{
651+
public void Bar(int i)
652+
{
653+
if (i == 0)
654+
Debug.Assert(true);
655+
else
656+
Debug.Assert(false);//8
657+
658+
659+
if (i == 1)
660+
Debug.Assert(true);
661+
else if (i == 2)
662+
Debug.Assert(false);//10
663+
664+
665+
if (i == 3)
666+
Debug.Assert(true);
667+
else if (i == 4)
668+
Debug.Assert(false);//14
669+
670+
671+
if (i == 5)
672+
Debug.Assert(true);
673+
else if (i == 6)
674+
Debug.Assert(false);
675+
else
676+
Debug.Assert(false);//16
677+
678+
679+
if (i == 7)
680+
if (i == 8)
681+
Debug.Assert(false);
682+
else
683+
Debug.Assert(false);
684+
else
685+
Debug.Assert(true);//18
686+
687+
688+
if (i == 9)
689+
if (i == 10)
690+
Debug.Assert(false);
691+
else
692+
Debug.Assert(false);
693+
else
694+
Debug.Assert(true);//21
695+
696+
697+
if (i == 11) if (i == 12)
698+
Debug.Assert(false);
699+
else
700+
Debug.Assert(false);
701+
else if (i == 13)
702+
Debug.Assert(true);//24
703+
704+
}
705+
}";
706+
707+
var batchFixedCode = @"using System.Diagnostics;
650708
public class TypeName
651709
{
652710
public void Bar(int i)
@@ -710,27 +768,30 @@ public void Bar(int i)
710768
{
711769
this.CSharpDiagnostic().WithLocation(8, 14),
712770
this.CSharpDiagnostic().WithLocation(10, 21),
713-
this.CSharpDiagnostic().WithLocation(10, 58).WithSeverity(DiagnosticSeverity.Hidden),
771+
this.CSharpDiagnostic().WithLocation(10, 58),
714772
this.CSharpDiagnostic().WithLocation(14, 26),
715773
this.CSharpDiagnostic().WithLocation(16, 21),
716-
this.CSharpDiagnostic().WithLocation(16, 58).WithSeverity(DiagnosticSeverity.Hidden),
717-
this.CSharpDiagnostic().WithLocation(16, 84).WithSeverity(DiagnosticSeverity.Hidden),
774+
this.CSharpDiagnostic().WithLocation(16, 58),
775+
this.CSharpDiagnostic().WithLocation(16, 84),
718776
this.CSharpDiagnostic().WithLocation(18, 21),
719-
this.CSharpDiagnostic().WithLocation(18, 33).WithSeverity(DiagnosticSeverity.Hidden),
720-
this.CSharpDiagnostic().WithLocation(18, 59).WithSeverity(DiagnosticSeverity.Hidden),
721-
this.CSharpDiagnostic().WithLocation(18, 85).WithSeverity(DiagnosticSeverity.Hidden),
722-
this.CSharpDiagnostic().WithLocation(21, 26).WithSeverity(DiagnosticSeverity.Hidden),
723-
this.CSharpDiagnostic().WithLocation(21, 52).WithSeverity(DiagnosticSeverity.Hidden),
777+
this.CSharpDiagnostic().WithLocation(18, 33),
778+
this.CSharpDiagnostic().WithLocation(18, 59),
779+
this.CSharpDiagnostic().WithLocation(18, 85),
780+
this.CSharpDiagnostic().WithLocation(21, 26),
781+
this.CSharpDiagnostic().WithLocation(21, 52),
724782
this.CSharpDiagnostic().WithLocation(21, 78),
725783
this.CSharpDiagnostic().WithLocation(23, 22).WithSeverity(DiagnosticSeverity.Hidden),
726784
this.CSharpDiagnostic().WithLocation(23, 35),
727-
this.CSharpDiagnostic().WithLocation(24, 18).WithSeverity(DiagnosticSeverity.Hidden),
785+
this.CSharpDiagnostic().WithLocation(24, 18),
728786
this.CSharpDiagnostic().WithLocation(24, 57),
729787
};
730788

789+
DiagnosticResult incrementalFixExpected = this.CSharpDiagnostic().WithLocation(50, 22).WithSeverity(DiagnosticSeverity.Hidden);
790+
731791
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
732-
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
733-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
792+
await this.VerifyCSharpDiagnosticAsync(incrementalFixedCode, incrementalFixExpected, CancellationToken.None).ConfigureAwait(false);
793+
await this.VerifyCSharpDiagnosticAsync(batchFixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
794+
await this.VerifyCSharpFixAsync(testCode, incrementalFixedCode, batchFixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
734795
}
735796

736797
/// <summary>
@@ -781,7 +842,7 @@ public void Bar(int i)
781842
DiagnosticResult[] expected =
782843
{
783844
this.CSharpDiagnostic().WithLocation(6, 21),
784-
this.CSharpDiagnostic().WithLocation(6, 46).WithSeverity(DiagnosticSeverity.Hidden),
845+
this.CSharpDiagnostic().WithLocation(6, 46),
785846
};
786847

787848
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
@@ -880,7 +941,7 @@ public void Bar(int i)
880941
DiagnosticResult[] expected =
881942
{
882943
this.CSharpDiagnostic().WithLocation(6, 21),
883-
this.CSharpDiagnostic().WithLocation(6, 33).WithSeverity(DiagnosticSeverity.Hidden),
944+
this.CSharpDiagnostic().WithLocation(6, 33),
884945
};
885946

886947
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

0 commit comments

Comments
 (0)