sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

Rule: S2189 detect infinite loops

Open fmallet opened this issue 7 years ago • 1 comments

Should implement RSPEC-2189

fmallet avatar Apr 11 '17 19:04 fmallet

I had a short look into this, taking RSPEC-2189 as a starting point.

class S2189
{
    void For() // Noncompliant 2190
    {
        for (; ; )
        {  // Noncompliant; end condition omitted
            // ...
        }
    }

    void While() // Noncompliant 2190
    {
        int j = 0;
        while (true)
        { // Noncompliant; end condition omitted
            j++;
        }
    }

    void Other()
    { 
        int k = 0;
        bool b = true;
        while (b) // Noncompliant S2589
        { // Noncompliant; b never written to in loop
            k++;
        }
    }

    IEnumerable<string> Yield()  // Noncompliant 2190
    {
        while (true)
        {
            yield return "test";
        }
    }
}

So, all issues are - indirect - covered by others. Is there value for implementing S2189 too? Though could argue that 2190 should not report on these, as no recursion (calling to self) is involved.

Corniel avatar Jun 12 '22 08:06 Corniel

Hi @Corniel,

sorry for the delay, thanks for the insightful analysis. As you pointed out, the rule S2189 is indeed covered for C# by S2190, in conjunction with S2589. We have also recently updated the title of S2190 to 'Loops and recursions should not be infinite', which now encompasses fully the scope of S2189.

Given this overlap, I'll move on and close this issue.