ערוך

שתף באמצעות


How to suppress code analysis warnings

This article covers the various ways you can suppress warnings from code analysis when you build your .NET app. You can suppress code quality rules, code style rules, and third-party analyzer rules using the information provided here.

Tip

If you're using Visual Studio as your development environment, the light bulb menu provides options that generate the code to suppress warnings for you. For more information, see Suppress violations.

Disable the rule

You can disable a rule that's causing a warning by setting its severity to none in an EditorConfig or AnalyzerConfig configuration file. This action disables the rule for your entire file or project, depending on the scope of the configuration file that you use.

[*.{cs,vb}]
dotnet_diagnostic.<rule-ID>.severity = none

For more information about rule severities, see Configure rule severity.

Use a preprocessor directive

Use a #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code.

    try { ... }
    catch (Exception e)
    {
#pragma warning disable CA2200 // Rethrow to preserve stack details
        throw e;
#pragma warning restore CA2200 // Rethrow to preserve stack details
    }
    Try
        ...
    Catch e As Exception
#Disable Warning CA2200 ' Rethrow to preserve stack details
        Throw e
#Enable Warning CA2200 ' Rethrow to preserve stack details
    End Try

Use the SuppressMessageAttribute

You can use a SuppressMessageAttribute to suppress a warning either in the source file or in a global suppressions file for the project (GlobalSuppressions.cs or GlobalSuppressions.vb). This attribute provides a way to suppress a warning in only certain parts of your project or file.

The two required, positional parameters for the SuppressMessageAttribute attribute are the category of the rule and the rule ID. The following code snippet passes "Usage" and "CA2200:Rethrow to preserve stack details" for these parameters.

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2200:Rethrow to preserve stack details", Justification = "Not production code.")]
private static void IgnorableCharacters()
{
    try
    {
        ...
    }
    catch (Exception e)
    {
        throw e;
    }
}

If you add the attribute to the global suppressions file, you scope the suppression to the desired level, for example "member". You specify the API where the warning should be suppressed using the Target property.

[assembly: SuppressMessage("Usage", "CA2200:Rethrow to preserve stack details", Justification = "Not production code.", Scope = "member", Target = "~M:MyApp.Program.IgnorableCharacters")]

Use the documentation ID for the API you want to reference in the Target attribute. For information about documentation IDs, see Documentation ID format.

To suppress warnings for compiler-generated code that doesn't map to explicitly provided user source, you must put the suppression attribute in a global suppressions file. For example, the following code suppresses a violation against a compiler-emitted constructor:

[module: SuppressMessage("Design", "CA1055:AbstractTypesDoNotHavePublicConstructors", Scope="member", Target="MyTools.Type..ctor()")]

See also