no-unsafe-finally

Disallow return, throw, break, and continue in finally blocks

error by default

A finally block runs after try and catch have already decided what happens next. A return, throw, break, or continue in it overrides that decision. The value that try returned is dropped, and an error that was on its way to the caller disappears.

This rule is on by default.

Configure#

On by default as an error. To turn it off:

bunfig.toml
[lint.rules]
"no-unsafe-finally" = "off" # or "warn"

Examples#

Reported. This function always returns {}, even when readConfig() throws:

example.js1 problem
function load() {
  try {
    return readConfig();
  } finally {
    return {};
  }
}
example.jsNo problems
function load() {
  try {
    return readConfig();
  } finally {
    closeFile();
  }
}

A statement that stays inside the finally block is fine, such as a continue for a loop in the block or a return in a nested function:

example.jsNo problems
try {
  save();
} finally {
  for (const handle of handles) {
    if (!handle.isOpen) continue;
    handle.close();
  }
  log(handles.map(handle => {
    return handle.name;
  }));
}