An else after two nested ifs without braces belongs to the inner if, whatever the indentation says
What it finds
In if (a) if (b) x(); else y(); the else belongs to the inner if, as the language specification says: y() runs when a holds and b does not. Written over several lines and indented under the outer if, it reads the other way.
Reported is an if without braces whose single statement is another if with an else, when the outer one has no else.
From the rule's source, dangling_else.rs.
class Branches { void run(boolean a, boolean b) { if (a) if (b) System.out.println("both"); elsetx0 reports: belongs to the inner if System.out.println("only a"); if (a) { if (b) { System.out.println("both"); } else { System.out.println("only a"); } } }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:dangling-else" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:dangling-else] reason="..."tx0 explain tx0/java:dangling-else shows the same in the terminal. Found a false positive? Report it.