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"));
}
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.
Anyway - feel free to delete this, since this is clearly a duplicate.