Rediger

Del via


Remove unnecessary suppression (IDE0370)

Property Value
Rule ID IDE0370
Title Remove unnecessary suppression
Category Style
Subcategory Unnecessary code rules (suppression preferences)
Applicable languages C#
Options None

Overview

This rule identifies unnecessary nullable warning suppressions using the null-forgiving operator (!). The null-forgiving operator tells the compiler that the value is not null, which suppresses warnings for nullable reference types. However, when the compiler can already determine that a value is not null, the null-forgiving operator is unnecessary and can be removed.

Example

// Code with violations.
#nullable enable

void ProcessValue()
{
    List<string> names = new()!;
}

// Fixed code.
#nullable enable

void ProcessValue()
{
    List<string> names = new(); // No suppression needed.
}

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 IDE0370
// The code that's violating the rule is on this line.
#pragma warning restore IDE0370

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

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

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

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

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

See also