Hello Petr,
This is a known limitation of the symbolic execution engine (SE) of the SonarJava analyzer, on which is based rule
S2259.
To give you a little bit more details, our SE engine usually assumes that for each loop it encounters, two options are possible:
- Enter the loop and explore its body (a certain number of times);
- Directly bypass it, considering the stop condition as being false from start.
Of course, this is a simplification and a wide approximation of the real loop behavior, which can shows its limits in cases similar to yours. "While(true) { }" is a particular case handled differently, discarding false branch, reason while the issue is disappearing in that case. We definitely want to rework the way we handle loop, in particular when dealing with integer literals, but nothing formally planned yet.
Anyway, I created the following ticket in order to keep in mind this specific limitation and the consequent FP:
SONARJAVA-2606
Cheers,
Michael