False positive with csharpsquid:S2583 with a constant and a manually incremented counter

  • What language is this for?
    C#
  • Which rule?
    csharpsquid:S2583
  • Are you using
    SonarQube Version 10.2 (build 77647)

This code (censored) which we use to fill in a table in a DOCX file:

const int itemsPerRow = 3;
var m = 0;
foreach (var item in items)
{
    // do something
    m++;
    if (m % itemsPerRow == 0) // <- here
    {
        // do something
    }
}

triggers this report:

Change this condition so that it does not always evaluate to ‘False’. Some code paths are unreachable.

The counter is incremented, so with enough items it will eventually satisfy the condition.

Can confirm, this also triggers on tuple unpacking:

public void Test()
{
    var x = 0;
    (x, var y) = (Random.Shared.Next(), Random.Shared.Next());

    if (x == 0) // Linter complains that this is always *True* instead of the expected False.
    {
        Console.WriteLine("hello world");
    }
}

Hi @m-gallesio and @ttphan. Thank you for providing the code samples. Both are confirmed to be False Positives. I’ve created Github issues for them. If you wish to track the progress then subscribe to these issues:

1 Like