False positive on S2583 and conditions looking at incrementing values inside of a loop

Please provide

  • Operating system: Windows 11
  • Visual Studio version: 17.8.7
  • SonarLint plugin version:
  • Programming language you’re coding in: C#
  • Is connected mode used: Yes
    • Connected to SonarCloud or SonarQube (and which version): SonarQube Enterprise Edition, version * 9.9.1 (build 69595)

In some cases , the rule “S2583 Conditionally executed code should be reachable” gives false positives in the case the if statement is on a variable that is changing because it is a loop counter, or it is explicitly incremented directly in the if statement.

Example code:

            int loopCount = 0;
            while (!this.backgroundCancellationSource.IsCancellationRequested)
                await this.DoBackgroundWork();
                await Task.Delay(TimeSpan.FromSeconds(15), this.backgroundCancellationSource.Token);
                if (++loopCount == 40)
                    loopCount = 0;
                    this.logger.LogInformation("External IdP Store background process still active, logging this message once every ten minutes");

This code is clearly incrementing the value such that it absolutely will get to 40.

Another example:

            int i = 0;
            while (sut.IdpCache.Count < 2)
                await Task.Delay(100);
                if (++i > 20)
                    throw new TestTimeoutException(2000);

This code is clearly incrementing the value such that it absolutely will get to 20.

1 Like

Hi there Doug, welcome to the community!

Thank you for your report. As a matter of fact, we just released enhancements to our engine that target S2583 among other rules.

This problem should be drastically reduced.

This version of the analyzer should be deployed on SonarCloud soon and will be in SonarQube 10.5.
Also, there is a specific topic for reporting false positive (under Clean Code/Report False-positive / False-negative. I moved your post under this topic.


1 Like