S2589 always triggers with AddRange

  • What language is this for? C#, .NET 8
  • Which rule? S2589
  • Why do you believe it’s a false-positive/false-negative?
    This rule seems to always trigger when AddRange is used, even when the parameter’s list is empty.
  • Using SonarLint - which IDE/version? Visual Studio 2022 17.11.2, SonarLint for Visual Studio 2022 8.2.2.98119
    • in connected mode with SonarQube or SonarCloud? No
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
List<int> i = [];

i.AddRange([]);

if (i.Count != 0) //S2589 triggers here : Change this condition so that it does not always evaluate to 'True'.
{
   Console.WriteLine("Foo");
}

This rule shouldn’t pop up here : i’s count is 0.

Hey there!

Thanks for the report. This seems to match a known issue tracked here: Fix S2589 FP: List count with AddRange() · Issue #8570 · SonarSource/sonar-dotnet · GitHub

It makes me wonder: shouldn’t there be a rule that alerts on adding known empty collections using AddRange()?

1 Like

Hello Colin,

Thank you for your response. I unfortunately see that there isn’t much activity on this issue as of late.

Is there an approximation for when this will be fixed? Thank you in advance!

Hi @Gameplushy, thanks for bringing this up again. The issue got miscategorized, leading to postponing work on it in the past. I fixed the categorization, and we will tackle the problem in the hopefully not-so-distant future.

1 Like

Hello Tim, thank you for responding.

I however do not see any recent label change in the issue, the last one goes back to February.
Are you talking about those changes? Or is it about changes you made recently but aren’t visible for the public?

Thank you once again.

I changed the Priority and Effort fields. Changes in these are not reflected in GitHub comments.

I changed:

  • Priority from Normal to High because there is user demand.
  • Effort from Large to Small because I do not know why it was Large in the first place.
1 Like