return, break or continue leaves a finally block

tx0/java:finally-jumpdefaultwarningbug

What it finds

return, break or continue that leaves a finally block discards whatever the try block was doing when the finally block began: an exception it threw is lost, and so is the value it returned.

A jump to a loop or label inside the finally block stays within it and is fine. A throw also replaces an exception, but is often how cleanup reports its own failure, and is left alone.

From the rule's source, finally_jump.rs.

Example from the rule's tests
class Streams {  int read(java.io.InputStream in) throws java.io.IOException {    try {      return in.read();    } finally {      in.close();      if (in.available() > 0) {        return -1;tx0 reports: return leaves a finally block      }    }  }   void drain(java.util.List<Runnable> tasks) {    for (Runnable task : tasks) {      try {        task.run();      } finally {        if (tasks.isEmpty()) {          break;tx0 reports: break leaves a finally block        }        for (int i = 0; i < 3; i++) {          if (i == 1) {            continue;          }          break;        }        Runnable r = () -> {          return;        };      }    }  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:finally-jump" = "off"

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:finally-jump] reason="..."

tx0 explain tx0/java:finally-jump shows the same in the terminal. Found a false positive? Report it.