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 IDE0370To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.IDE0370.severity = noneTo 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 = noneFor more information, see How to suppress code analysis warnings.