SonarCloud S2589 C# False Positive

The following code will give a false positive for S2589.

public void MyTestMethod(int[] args)
	object x = null;
	object y = null;

	foreach (int arg in args)
		if (arg == 1)
			x = new object();
		else if (arg == 2)
			y = new object();

	if (x == null || y == null)


Hello @StephenLarkin

Are you sure about that? Look here, I c/p your code and analyzed it and nothing is reported. regarding S2589.


1 Like

Ah, that’s interesting. That code was an example that I was given by a team member. I’ll go back to them and see if I can get a repro from them.

In the meantime, there were a number of false positives that I found in our code base regarding this rule when I was investigating this yesterday.

It seems to follow the same pattern as above.

hi @StephenLarkin

I’m afraid this screenshot is not enough to tell why the FP is happening. We have a bunch of known FPs for S2589 .

To tell if it’s a new one or one of the existing ones, we’d need a reproducer.