Skip to content

Commit 0d96701

Browse files
committed
Only report SA1206 once on a given identifier
1 parent d3ec61c commit 0d96701

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1206CodeFixProviderUnitTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public async Task VerifyKeywordReorderingInMethodDeclarationAsync()
116116
DiagnosticResult[] expected =
117117
{
118118
Diagnostic().WithLocation(1, 38).WithArguments("static", "new"),
119-
Diagnostic().WithLocation(1, 45).WithArguments("public", "static"),
120119
Diagnostic().WithLocation(1, 45).WithArguments("public", "new"),
121120
};
122121
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1206UnitTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public async Task TestKeywordsWithExpressionBodiedMemberAndAsyncKeywordAsync()
4040
DiagnosticResult[] expected = new[]
4141
{
4242
Diagnostic().WithLocation(3, 11).WithArguments("static", "async"),
43-
Diagnostic().WithLocation(3, 18).WithArguments("public", "static"),
4443
Diagnostic().WithLocation(3, 18).WithArguments("public", "async"),
4544
};
4645

@@ -75,7 +74,6 @@ static public void ThirdMethod() {}
7574
Diagnostic().WithLocation(7, 32).WithArguments("protected", "async"),
7675
Diagnostic().WithLocation(8, 16).WithArguments("static", "new"),
7776
Diagnostic().WithLocation(9, 9).WithArguments("static", "new"),
78-
Diagnostic().WithLocation(9, 16).WithArguments("public", "static"),
7977
Diagnostic().WithLocation(9, 16).WithArguments("public", "new"),
8078
Diagnostic().WithLocation(10, 13).WithArguments("public", "virtual"),
8179
Diagnostic().WithLocation(11, 12).WithArguments("public", "extern"),
@@ -160,7 +158,6 @@ public async Task TestKeywordsWithOperatorDeclarationsAsync()
160158
Diagnostic().WithLocation(3, 12).WithArguments("public", "extern"),
161159
Diagnostic().WithLocation(3, 19).WithArguments("static", "extern"),
162160
Diagnostic().WithLocation(4, 12).WithArguments("static", "extern"),
163-
Diagnostic().WithLocation(4, 19).WithArguments("public", "static"),
164161
Diagnostic().WithLocation(4, 19).WithArguments("public", "extern"),
165162
Diagnostic().WithLocation(5, 19).WithArguments("static", "extern"),
166163
};

StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1206DeclarationKeywordsMustFollowOrder.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,26 @@ private static void CheckModifiersOrderAndReportDiagnostics(SyntaxNodeAnalysisCo
9595
{
9696
var currentModifierType = GetModifierType(modifier);
9797

98+
bool reportPreviousModifier = false;
99+
bool reportPreviousOtherModifier = false;
98100
if (CompareModifiersType(currentModifierType, previousModifierType) < 0)
99101
{
100-
context.ReportDiagnostic(Diagnostic.Create(Descriptor, modifier.GetLocation(), modifier.ValueText, previousModifier.ValueText));
102+
reportPreviousModifier = true;
101103
}
102104

103105
if (AccessOrStaticModifierNotFollowingOtherModifier(currentModifierType, previousModifierType) && otherModifiersAppearEarlier)
104106
{
105-
context.ReportDiagnostic(Diagnostic.Create(Descriptor, modifier.GetLocation(), modifier.ValueText, previousOtherModifier.ValueText));
107+
reportPreviousOtherModifier = true;
108+
}
109+
110+
if (reportPreviousModifier || reportPreviousOtherModifier)
111+
{
112+
// Note: Only report one diagnostic per modifier. If both diagnostics apply, report the diagnostic
113+
// relative to the earlier modifier.
114+
var reportedModifier = reportPreviousModifier && (!reportPreviousOtherModifier || previousModifier.SpanStart < previousOtherModifier.SpanStart)
115+
? previousModifier.ValueText
116+
: previousOtherModifier.ValueText;
117+
context.ReportDiagnostic(Diagnostic.Create(Descriptor, modifier.GetLocation(), modifier.ValueText, reportedModifier));
106118
}
107119

108120
if (!otherModifiersAppearEarlier && currentModifierType == ModifierType.Other)

0 commit comments

Comments
 (0)