S4158 and Actions

Then tell us:

  • What language is this for?
    C#
  • Which rule?
    S4158
  • Why do you believe it’s a false-positive/false-negative?
    The collection will not be empty (assuming code works correctly of course)
  • Are you using
    • SonarLint - 8.0.0.92083 for VS 2022
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
    A crude example showcasing how the S4158 will be triggered, even if the collection is guaranteed to contain elements at that point.
        private void S4158Test()
        {
            var collection = new List<string>();
            var action = () => collection.Add("test");
            action();
            Console.WriteLine(collection.Exists(x => x == "test"));
        }

Hey there.

Thanks for the report. I think you’re facing one of these known false-postives:

You’re right! This appears to be a duplicate.

I guess I reported this because the first time I’ve seen this issue was… some time ago, and I’ve seen SonarLint updated a few times since then with this still being a false-positive so I figured I’d report this. :sweat_smile:

Anyway - feel free to delete this, since this is clearly a duplicate.

No worries. We appreciate the report anyways. :slight_smile: It helps us prioritize issues if we know more users are reporting them.

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