NormalCompletionVisitor
When deciding into which scope pattern variables should be introduced, it is sometimes necessary to determine whether a statement can complete normally {@see https://docs.oracle.com/javase/specs/jls/se22/html/jls-14.html#jls-14.22}. The JLS specifies that a statement can complete normally only if it is reachable and specifies rules for what it means for a statement to be reachable, but that part can be ignored in JavaParser since having unreachable code results in a compilation error and is thus not supported. This means that all of the rules are implemented with the assumption that provided nodes are reachable. An example of where this is needed is for the following rule regarding pattern variables introduced by if-statements. 6.3.2.2. if Statements {@see https://docs.oracle.com/javase/specs/jls/se22/html/jls-6.html#jls-6.3.2.2}: The following rules apply to a statement if (e) S (§14.9.1): A pattern variable is introduced by if (e) S iff (i) it is introduced by e when false and (ii) S cannot complete normally. This means that in this example:
if (!(x instanceof Foo f)) {
return;
}
System.out.println(f);
f will be in scope for the println call since the block making up the then-block of the if statement (S in the rule above) cannot complete normally (since the last statement, return, cannot complete normally). But, in this example:
if (!(x instanceof Foo f)) { }
f is not introduced by the if statement since the empty then-block can complete normally.