docs/articles/nunit-analyzers/NUnit2058.md
| Topic | Value |
|---|---|
| Id | NUnit2058 |
| Severity | Warning |
| Enabled | True |
| Category | Assertion |
| Code | MisusedConstraintsAnalyzer |
The constraint didn't take the operator precedence into account.
We have seen users wanting to test for an equivalent of !string.IsNullOrEmpty() using Is.Not.Null.Or.Empty.
Assert.That(name, Is.Not.Null.Or.Empty);
Because of the operator precedence this actually means Is.Not.Null | Is.Empty.
The Is.Empty part will only be tested when the item under test is null.
Depending on the type this either returns false or throws an Exception.
The code fix associated with this rule will convert this to test for both Not.Null and Not.Empty:
Assert.That(name, Is.Not.Null.And.Not.Empty);
Configure the severity per project, for more info see MSDN.
# NUnit2058: The constraint is misused
dotnet_diagnostic.NUnit2058.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
#pragma warning disable NUnit2058 // The constraint is misused
Code violating the rule here
#pragma warning restore NUnit2058 // The constraint is misused
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit2058 // The constraint is misused
[SuppressMessage][System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2058:The constraint is misused",
Justification = "Reason...")]