False positive S1751 on some continue statements

Some continue statements are flagged as “Refactor the containing loop to do more than one iteration”, even though a continue statement cannot reduce the number of iterations a loop does. break, goto, return, and throw (at least) can reduce the number of iterations, but not continue.

static void Foo(string[] list) {
    foreach (string s in list) {
        continue; // Incorrect S1751 (also a correct S3626 in this sample, but not in the production code)
    }
}

I’m using SonarQube 8.26.0.14164 on Visual Studio 17.14.14 and Windows 11 24H2.

Hey Dale.

Thanks for the report.

This looks like NET-1690 Fix S1751 FP: Jump statements (continue, break, return) to separate local functions · Issue #7987 · SonarSource/sonar-dotnet · GitHub. Can you take a look?

1 Like

They’re similar, but I’m only concerned with the use of S1751 on continue statements, while that also discusses return and break.

The issue I see here is that neither the error text nor description match the behavior of a continue statement. The fix that seems reasonable to me in this case, depending on the intent of S1751, is either
(a) never issue S1751 on a continue statement, because continue statements never reduce the loop count (possibly also introduce a new diagnostic for unconditional continues), or
(b) change the text of S1751 to something like “Refactor the containing loop to perform this jump statement conditionally,” and change C# static code analysis (the S1751 help page) to match the intended concern of unconditional jump statements rather than single-iteration loops.

1 Like

Hi @dalestan,
Thanks for raising this. I’ve had a look at this and agree this is an FP.
I’ve added a ticket to our backlog.

Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.