Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nalka0 opened this issue Sep 30, 2024 · 1 comment
Labels
Area: C# C# rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.

Comments

@nalka0
Copy link
Contributor

nalka0 commented Sep 30, 2024

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
@CristianAmbrosini
Copy link
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!

@CristianAmbrosini CristianAmbrosini added Area: C# C# rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. labels Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
None yet
Development

No branches or pull requests

2 participants