Redaguoti

Bendrinti naudojant


Remove unnecessary suppression (IDE0079)

Property Value
Rule ID IDE0079
Title Remove unnecessary suppression
Category CodeQuality
Subcategory Unnecessary code rules (suppression preferences)
Applicable languages C# and Visual Basic
Options dotnet_remove_unnecessary_suppression_exclusions

Overview

This rule flags unnecessary pragma and SuppressMessageAttribute attribute suppressions in source.

Source suppressions suppress violations of compiler and analyzer rules in specific places but not in other parts of the source code. You generally use them to suppress false positives or less important violations that you don't intend to fix. However, suppressions often become stale. This can happen if a rule is fixed to prevent false positives or you refactor your code and, in doing so, render the suppressions redundant. This rule helps to identify redundant suppressions, which can be removed.

Note

Even if you enable code style rules on build, this rule is not enabled. It only surfaces in the Visual Studio editor.

Example

using System.Diagnostics.CodeAnalysis;

class C1
{
    // Necessary pragma suppression
#pragma warning disable IDE0051 // IDE0051: Remove unused member
    private int UnusedMethod() => 0;
#pragma warning restore IDE0051

    // IDE0079: Unnecessary pragma suppression
#pragma warning disable IDE0051 // IDE0051: Remove unused member
    private int UsedMethod() => 0;
#pragma warning restore IDE0051

    public int PublicMethod() => UsedMethod();
}

class C2
{
    // Necessary SuppressMessage attribute suppression
    [SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "<Pending>")]
    private int _unusedField;

    // IDE0079: Unnecessary SuppressMessage attribute suppression
    [SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "<Pending>")]
    private int _usedField;

    public int PublicMethod2() => _usedField;
}

Options

Options specify the behavior that you want the rule to enforce. For information about configuring options, see Option format.

dotnet_remove_unnecessary_suppression_exclusions

Property Value Description
Option name dotnet_remove_unnecessary_suppression_exclusions
Option values , separated list of rule IDs or categories (prefixed with category:) Excludes suppressions for the listed rules
all Disables the rule (all rule IDs excluded)
none Enables the rule for all rules (no exclusions)
Default option value none
using System.Diagnostics.CodeAnalysis;

class C1
{
    // 'dotnet_remove_unnecessary_suppression_exclusions = IDE0051'

    // Unnecessary pragma suppression, but not flagged by IDE0079
#pragma warning disable IDE0051 // IDE0051: Remove unused member
    private int UsedMethod() => 0;
#pragma warning restore IDE0051

    public int PublicMethod() => UsedMethod();
}

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable IDE0079
// The code that's violating the rule is on this line.
#pragma warning restore IDE0079

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.IDE0079.severity = none

To disable this entire category of rules, set the severity for the category to none in the configuration file.

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-CodeQuality.severity = none

For more information, see How to suppress code analysis warnings.

See also