Incorrect unreachable code warning for some if-conditions

  • Operating system: Windows 10
  • SonarLint plugin version:
  • Programming language you’re coding in: C#
  • Is connected mode used: no

for some conditionals the unreachable code is evaluated incorrectly.

In the following snippet the condition else if (stateA) gives warning “S2583 Change this condition so that it does not always evaluate to ‘False’”.

bool stateA = false;
bool stateB = true;

foreach (var item in items)
	if (Func(item))
		stateA = true;
		stateB = false;

if (stateB) { ... }
else if (stateA) { ... }
else { ... }

If I changed the order of conditions to make the second branch actually unreachable like this:

if (stateA) { ... }
else if (stateB) { ... }
else { ... }

then it doesn’t give any warning.

Hi Dalibor, thanks a lot for your report with a clear reproducer!
This has the same cause as this issue:

You can track the development status on Github and see when it gets released.
Have a nice day!

@dalibac This specific issue should be fixed by now.