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

False Positive for S4158 when using a delegate to add to the collection #5544

Closed
davidmilligan opened this issue Apr 5, 2022 · 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

@davidmilligan
Copy link

S4158 is incorrectly calling out that the collection is empty when a delegate to the add method is passed to another method

        public static void Main()
        {
            var list = new List<string>();
            AddSomeStuff(list.Add);
            list.ForEach(Console.WriteLine); //< - S4158 here, but the program actually prints hello world
        }

        private static void AddSomeStuff(Action<string> add)
        {
            add("hello");
            add("world");
        }
@costin-zaharia-sonarsource
Copy link
Member

Closing this issue as a duplicate of #4261

@costin-zaharia-sonarsource costin-zaharia-sonarsource 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 Jun 8, 2022
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