False positive for S4158


The following code gives a warning on the last line

  • S4158 - Empty collections should not be accessed or iterated

But the list is not empty.

    public void TestRule()
      var lst1 = new List<int>() { 1, 2, };
      var lst2 = new List<int>();
      lst1.ForEach(i => lst2.Add(i));
      lst2.ForEach(i => System.Diagnostics.Debug.WriteLine(i));


  • Visual Studio 17.8.0
  • SonarLint 7.4.0

Kind regards,


Hello @Valentijn!

Unfortunately, this is a well-known issue.
Currently, our symbolic execution engine does not explore lambdas, hence it fails to detect the ‘Add’ invocation within it.

Improving our engine to support lambdas is part of our future development plans, you can track the progress on this issue on GitHub, I’ll make sure to mention your post in the comments.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.