Please provide
- Operating system: Windows 11
- Visual Studio version: 17.8.7
- SonarLint plugin version: 7.6.0.83110
- 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.