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));

Hi,

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?

 
Thx,
Ann

Visual Studio 17.7.0
Sonar Lint 7.1.0.75242

1 Like

Hi,

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

 
Ann

Hello @groogiam,

Thank you for your feedback.

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

Have a good day,

1 Like