S4158: "Empty collections should not be accessed or iterated" False positive with local method

var queue = new Queue<string>();
while (queue.Count > 0) queue.Dequeue();
void UpdateQueue() => queue.Enqueue("foo");

Now S4158 is reported on Dequeue because it doesn’t recognize the call to Enqueue. Passing the queue to the local method seems to be a workaround, regardless of the method using this parameter:

var queue = new Queue<string>();
while (queue.Count > 0) queue.Dequeue();
void UpdateQueue(object _) => queue.Enqueue("foo");

Hey there

Thanks for the report.

Please read this thread about reporting a false-positive/false-negative

Specifically, we’re missing what product(s) you’re using, and if relevant what version of those products.

And, I think you are probably facing the false-postive already reported here. Fix S4158 FP: Collection filled in a local function · Issue #4478 · SonarSource/sonar-dotnet · GitHub