S2583 "Change this condition so that it does not always evaluate to 'false'..." False positive

Then tell us:

  • What language is this for?

C#

  • Which rule?

S2583 Change this condition so that it does not always evaluate to ‘false’; some subsequent code is never executed.

  • Why do you believe it’s a false-positive/false-negative?

False positive

  • Are you using

    • SonarQube - which version? - Enterprise Edition Version 9.9.4 (build 87374)
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

Sadly I can’t give the actual code due to IP restrictions. But it goes something like this:

static void DoSomething(string s)
{
    var inWhiteSpace = true;
    var followingWhiteSpace = false; // The variable starts off false
    foreach (var c in s)
    {
        if (char.IsWhiteSpace(c))
        {
            if (inWhiteSpace) // This will be false after the first non white space.
            {
                continue;
            }

            followingWhiteSpace = true; // The variable is set to true
            inWhiteSpace = true;
            continue;
        }

        inWhiteSpace = false;

        if (followingWhiteSpace) // S2583 reported here
        {
             // Do something
             followingWhiteSpace = false;
        }

        // Do something else
    }
}

Hello @mgbrown, thank you for reporting this.

I confirm it’s a false positive. However, it has been fixed since the sonar-dotnet analyzer 9.22 (see related issue here).
SonarQube 9.9.4 comes with sonar-dotnet 8.51. You should update to at least SonarQube 10.5.0 to profit from this and more fixes.

Thanks!

1 Like

Where as I should be able to, that rather depends upon the people that manage our SonarQube install. I’ll have a chat with them see how far I get.

1 Like