Try-Catch confuses detection of boolean changes (S2589)

Using SonarAnalyzer.CSharp 7.16.0.8981 and SonarLint for VS 2019 4.12.0.0

Shortest reproduction I can manage, not actual use case.

bool blip = false;
try
{
	var rnd = new System.Random();
	if (rnd.Next() % 100 > 50) throw new Exception();
	blip = true;
}
catch { /* Ignore */  }

if (blip) Console.WriteLine("blip");

S2589 is triggered for the last line claiming it’s always true despite the potentially thrown exception causing it to be false.

Possibly affects other data types, but I didn’t test them.

thanks @mkahvi - I added your repro to the existing #2600

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