Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

How to suppress code analysis warnings

Feedback

In this article

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, thelight bulb menu provides options that generate the code to suppress warnings for you. For more information, seeSuppress violations.

Disable the rule

You can disable a rule that's causing a warning by setting its severity tonone in an EditorConfig or AnalyzerConfigconfiguration 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, seeConfigure rule severity.

Use a preprocessor directive

Use a#pragma warning (C#) orDisable (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 aSuppressMessageAttribute to suppress a warning either in the source file or in a global suppressions file for the project (GlobalSuppressions.cs orGlobalSuppressions.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 theSuppressMessageAttribute attribute are thecategory of the rule and therule 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, youscope the suppression to the desired level, for example"member". You specify the API where the warning should be suppressed using theTarget property.

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

Use thedocumentation ID for the API you want to reference in theTarget attribute. For information about documentation IDs, seeDocumentation 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",)]

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?