- Reported using C# plugin version 7.5 (build 6605) in SonarQube 7.3
I believe the rule csharpsquid:S2583
is overaggressively flagging a specific scenario as false-positive, around null-or-empty checking for strings. The standard to verify a string ‘has content’ in C# is the built-in function String.IsNullOrWhitespace(). However, there is often a valid reason for having previously nullchecked a string - for example, determining whether to throw an ArgumentNullException or an ArgumentException at the beginning of the function.
However, the rule flags this as having ‘unused’ code, even though realistically speaking there is no other pattern to follow. There is no built-in function to check if a string is just whitespace or just empty without doing the bundled nullcheck.
public void MethodFoo(String someInput)
{
if (someInput == null)
throw new ArgumentNullException(nameof(someInput));
if (String.IsNullOrWhitespace(someInput))
someInput = "default";
.....
}