S2583 gives a false positive

Make sure to read this post before raising a thread here:

Then tell us:

  • What language is this for? C#
  • Which rule? S2583
  • Why do you believe it’s a false-positive/false-negative? The example below demonstrates as such
  • Are you using
    • SonarCloud? Yes
    • SonarQube - which version?
    • SonarLint - which IDE/version? VS2022 and 8.5.0.10497
      • in connected mode with SonarQube or SonarCloud?
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
        List<int> ints = new();

        List<int> retrievedInts = new();

        ints.AddRange(retrievedInts);

        if (ints.Count > 0) // This causes S2583 to be raised but can't always be true because ints is empty
        {
            throw new Exception();
        }

I fully agree, this should be a FP: .AddRange () can result in adding no items at all, and from this code it is even guaranteed not to have any items.

This is fixed internally. It will be released in the not-so-distant future!

1 Like