Linter · Rules
no-unsafe-finally
Disallow return, throw, break, and continue in finally blocks
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:
function load() {
try {
return readConfig();
} finally {
return {};
}
}
Not reported:
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:
try {
save();
} finally {
for (const handle of handles) {
if (!handle.isOpen) continue;
handle.close();
}
log(handles.map(handle => {
return handle.name;
}));
}