Remove unnecessary suppression (IDE0079)

Property Value
Rule ID IDE0079
Title Remove unnecessary suppression
Category Style
Subcategory Unnecessary code rules
Applicable languages C# and Visual Basic

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 these false positives or user code is 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;
}

dotnet_remove_unnecessary_suppression_exclusions

Property Value
Option name dotnet_remove_unnecessary_suppression_exclusions
Option values , separated list of rule IDs or rule categories (prefixed with category:) whose suppressions must be excluded from analysis

all - disables the rule

none - enables the rule for all rule IDs and rule categories
Default option value none

Example

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();
}

See also