return, break or continue leaves a finally block

tx0/ts:unsafe-finallydefaultwarningbug

What it finds

return, break or continue that leaves a finally block replaces whatever the try was doing: an exception it threw is silently lost, and a value it returned is replaced.

Statements whose target is inside the finally block are fine: a break out of a loop in it, or anything in a function defined in it. A throw replaces an exception too, but still reports an error, as an assertion in a finally block does on purpose, and is left alone.

From the rule's source, unsafe_finally.rs.

Example from the rule's tests
function read(open: () => string, close: () => void) {  try {    return open();  } finally {    close();    return "";tx0 reports: return in a finally block discards  }} function loop(items: string[], close: () => void) {  for (const item of items) {    try {      close();    } finally {      if (item) {        continue;tx0 reports: continue in a finally block discards      }      for (const other of items) {        if (other) break;      }      items.forEach(i => { return i; });      if (!item) throw new Error("closed");    }  }}
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/ts:unsafe-finally" = "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/ts:unsafe-finally] reason="..."

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