Skip to content

NET-1543 Fix S2583 FP: List is filled in local function, then checking its Count is > 0 #9671

Open

Description

@nalka0

Description

There's a false positive for S2583 with the below snippet

Repro steps

public static void S2583()
{
    List<int> list = new();
    foreach (var item in Enumerable.Range(0, 5))
    {
        if (item % 2 == 0)
        {
            LocalFunction(item);
        }

        void LocalFunction(int added)
        {
            list.Add(added);
        }
    }

    if (list.Count > 0) // S2583 false positive
    {
        Console.WriteLine("This code is reachable");
    }
}

Expected behavior

No occurence of S2583

Actual behavior

S2583 is raised on if (list.Count > 0)

Known workarounds

None

Related information

  • Microsoft Visual Studio Community 2022 - Version 17.11.4
  • .NET 6 and 8
  • SonarScanner for .NET version 9.0.0
  • Windows 11

Activity

  1. CristianAmbrosini commented on Oct 2, 2024

    @CristianAmbrosini
    Contributor

    Hi @nalka0!

    I confirm this as a FP.
    Currently, we do not track collections filled within local methods. I'll add a reproducer to track them in our repo.
    Thanks for the feedback!

  2. added a commit that references this issue on Nov 28, 2024
  3. added a commit that references this issue on Nov 29, 2024
  4. added a commit that references this issue on Nov 29, 2024
  5. pavel-mikula-sonarsource commented on Jun 5, 2025

    @pavel-mikula-sonarsource
    Contributor

    Internal ticket NET-1543

  6. changed the title [-]Fix S2583 FP: List is filled in local function, then checking its Count is > 0[/-] [+]NET-1543 Fix S2583 FP: List is filled in local function, then checking its Count is > 0[/+] on Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    False PositiveRule IS triggered when it shouldn't be.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions