Why is this an issue?

The finally block is a part of a try…​catch…​finally statement, which allows you to handle errors and perform cleanup operations regardless of whether an exception is thrown or not. The finally block is executed regardless of whether an exception occurs or not, and it is placed after the try and catch blocks.

Having a jump statement, such as return, break, continue, or throw, inside a finally block can lead to unexpected and undesirable behavior, making your code difficult to understand and maintain. While it’s not inherently forbidden to use jump statements in finally blocks, it is generally discouraged for the following reasons:

This rule reports on all usages of jump statements from a finally block. Even if it’s guaranteed that no unhandled exception can happen in try or catch blocks, it’s not recommended to use any jump statements inside the finally block to have the logic there limited to the "cleanup".

async function foo() {
    let result, connection;
    try {
        connection = await connect();
        result = connection.send(1);
    } catch(err) {
        console.error(err.message);
    } finally {
        if (connection) {
            connection.close();
        }
        return result; // Noncompliant: Jump statement 'return' in the 'finally' block
    }
}

While there might be rare cases where using jump statements in a finally block is necessary, it’s generally recommended to avoid it whenever possible. Instead, use the finally block only for cleanup operations and critical tasks that should always be executed, regardless of exceptions or return values.

async function foo() {
    let result, connection;
    try {
        connection = await connect();
        result = connection.send(1);
    } catch(err) {
        console.error(err.message);
    } finally {
        if (connection) {
            connection.close();
        }
    }
    return result;
}

Resources

Documentation