Skip to content

Commit c95d338

Browse files
authored
Merge pull request #2863 from sharwell/effective-severity
Use DiagnosticDescriptor.GetEffectiveSeverity to check for suppressions
2 parents 31d1955 + 4414f0c commit c95d338

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/DiagnosticOptionsHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace StyleCop.Analyzers.Helpers
88
{
9+
using System;
910
using System.Collections.Immutable;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.Diagnostics;
@@ -56,12 +57,12 @@ internal static bool IsAnalyzerSuppressed(this Compilation compilation, Diagnost
5657
/// <returns>True if the diagnostic is currently suppressed.</returns>
5758
internal static bool IsAnalyzerSuppressed(this CompilationOptions compilationOptions, DiagnosticDescriptor descriptor)
5859
{
59-
switch (compilationOptions.SpecificDiagnosticOptions.GetValueOrDefault(descriptor.Id))
60+
switch (descriptor.GetEffectiveSeverity(compilationOptions))
6061
{
6162
case ReportDiagnostic.Suppress:
6263
return true;
6364
case ReportDiagnostic.Default:
64-
return !descriptor.IsEnabledByDefault;
65+
throw new InvalidOperationException("This should be unreachable.");
6566
default:
6667
return false;
6768
}

StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1304NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ private static void HandleFieldDeclaration(SyntaxNodeAnalysisContext context)
8484
{
8585
// SA1307 is taken precedence here. SA1307 should be reported if the field is accessible.
8686
// So if SA1307 is enabled this diagnostic will only be reported for internal fields.
87-
if (context.SemanticModel.Compilation.Options.SpecificDiagnosticOptions
88-
.GetValueOrDefault(SA1307AccessibleFieldsMustBeginWithUpperCaseLetter.DiagnosticId, ReportDiagnostic.Default) != ReportDiagnostic.Suppress)
87+
if (!context.IsAnalyzerSuppressed(SA1307AccessibleFieldsMustBeginWithUpperCaseLetter.Descriptor))
8988
{
9089
return;
9190
}

StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1307AccessibleFieldsMustBeginWithUpperCaseLetter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ internal class SA1307AccessibleFieldsMustBeginWithUpperCaseLetter : DiagnosticAn
3737
private const string Description = "The name of a public or internal field in C# does not begin with an upper-case letter.";
3838
private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1307.md";
3939

40-
private static readonly DiagnosticDescriptor Descriptor =
40+
#pragma warning disable SA1202 // Elements should be ordered by access
41+
internal static readonly DiagnosticDescriptor Descriptor =
4142
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.NamingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
43+
#pragma warning restore SA1202 // Elements should be ordered by access
4244

4345
private static readonly Action<SyntaxNodeAnalysisContext> FieldDeclarationAction = HandleFieldDeclaration;
4446

0 commit comments

Comments
 (0)