Skip to content

eslint/no-unsafe-finally Correctness

This rule is turned on by default.

What it does

Disallow control flow statements in finally blocks

Why is this bad?

JavaScript suspends the control flow statements of try and catch blocks until the execution of finally block finishes. So, when return, throw, break, or continue is used in finally, control flow statements inside try and catch are overwritten, which is considered as unexpected behavior.

Example

javascript
// We expect this function to return 1;
(() => {
  try {
    return 1; // 1 is returned but suspended until finally block ends
  } catch (err) {
    return 2;
  } finally {
    return 3; // 3 is returned before 1, which we did not expect
  }
})();

// > 3

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny no-unsafe-finally
json
{
  "rules": {
    "no-unsafe-finally": "error"
  }
}

References

Released under the MIT License.