Remove unnecessary suppression (IDE0079)

Property Value
Rule ID IDE0079
Title Remove unnecessary suppression
Category CodeQuality
Subcategory Unnecessary code rules
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 are meant to suppress violations of compiler and analyzer rules for specific parts of source code, without disabling the rules in the other parts of the code. They are generally added to suppress false positives or less important violations that user does not intend to fix. Suppressions can frequently become stale, either due to the rules getting fixed to prevent false positives or user code being refactored to render the suppressions redundant. This rule helps identify such redundant suppressions, which can be removed.

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