C# S4158 False Positive

This rule appears to have some logic issues. It flags positive in the below snippet. It seems like using the object initializer in the Add call causes it to malfunction.

var myList = new List<SomeObject>();

foreach(var other in Enumerable.Range(0,100))
    myList.Add(new SomeObject { Value = other});

myList.Foreach(i => Console.WriteLine(i));


Thanks for this report. I’ll need a few more details to make sure you’re seeing this in the latest version. Can you provide your SonarLint flavor / IDE, and SonarLint version, please?


Visual Studio 17.7.0
Sonar Lint

1 Like


Thanks for the detail. I’ve flagged this for the language experts.


Hello @groogiam,

Thank you for your feedback.

This is indeed an FP in our latest release, as of today.
Fortunately, this is already fixed and should be available in the next release.

Have a good day,

1 Like