False positive for S2583 - Conditionally executed code should be reachable

Hi,

The following code gives a warning for the if-statement but in this case it will always be false instead of true

In the real code the lists come from functions that may or may not return lists that contain items so we need the if to check for items.

    [Test]
    public void TestAddRangeEmptyList()
    {
      var list = new List<int>();
      list.AddRange(new List<int>());
      System.Diagnostics.Debug.WriteLine($"Count:{list.Count}");
      if (list.Count != 0)
      {
        System.Diagnostics.Debug.WriteLine("List is not empty");
        return;
      }

      System.Diagnostics.Debug.WriteLine("List is empty");
    }

Then tell us:

  • What language is this for?
    C#

  • Which rule?
    S2583 Conditional expressions which are always true or false can lead to unreachable code.

  • Why do you believe it’s a false-positive/false-negative?
    Adding an empty list to a list does not mean the list contains items

  • Are you using

    • SonarQube
      Server ID: 86E1FA4D-AVqpBTuKcamw9_u2b0kc
      Version: 10.6.0.92116
      Date: 2024-09-19

    • SonarLint for Visual Studio 2022 (8.3.0.10161)

      • in connected mode with SonarQube

Hello @Valentijn,

This is a known issue that has been recently fixed.

Have a nice day!