no-useless-assignment

不使用值时禁止变量赋值

维基百科描述了 “死存储” 如下:

¥Wikipedia describes a “dead store” as follows:

在计算机编程中,已分配值但不被任何后续指令读取的局部变量称为死存储。

¥In computer programming, a local variable that is assigned a value but is not read by any subsequent instruction is referred to as a dead store.

“死存储” 浪费处理和内存,因此最好删除对变量不必要的赋值。

¥"Dead stores" waste processing and memory, so it is better to remove unnecessary assignments to variables.

另外,如果作者打算使用该变量,则死存储周围可能存在错误。例如,

¥Also, if the author intended the variable to be used, there is likely a mistake around the dead store. For example,

  • 你应该使用存储值但忘记这样做。

    ¥you should have used a stored value but forgot to do so.

  • 你在要存储的变量名称中犯了错误。

    ¥you made a mistake in the name of the variable to be stored.

let id = "x1234";    // this is a "dead store" - this value ("x1234") is never read

id = generateId();

doSomethingWith(id);

规则详情

¥Rule Details

此规则旨在在未使用值时报告变量分配。

¥This rule aims to report variable assignments when the value is not used.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/* eslint no-useless-assignment: "error" */

function fn1() {
    let v = 'used';
    doSomething(v);
    v = 'unused';
}

function fn2() {
    let v = 'used';
    if (condition) {
        v = 'unused';
        return
    }
    doSomething(v);
}

function fn3() {
    let v = 'used';
    if (condition) {
        doSomething(v);
    } else {
        v = 'unused';
    }
}

function fn4() {
    let v = 'unused';
    if (condition) {
        v = 'used';
        doSomething(v);
        return
    }
}

function fn5() {
    let v = 'used';
    if (condition) {
        let v = 'used';
        console.log(v);
        v = 'unused';
    }
    console.log(v);
}

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/* eslint no-useless-assignment: "error" */

function fn1() {
    let v = 'used';
    doSomething(v);
    v = 'used-2';
    doSomething(v);
}

function fn2() {
    let v = 'used';
    if (condition) {
        v = 'used-2';
        doSomething(v);
        return
    }
    doSomething(v);
}

function fn3() {
    let v = 'used';
    if (condition) {
        doSomething(v);
    } else {
        v = 'used-2';
        doSomething(v);
    }
}

function fn4() {
    let v = 'used';
    for (let i = 0; i < 10; i++) {
        doSomething(v);
        v = 'used in next iteration';
    }
}

该规则不会报告从未读取过的变量。因为它显然是一个未使用的变量。如果你希望报告,请启用 no-unused-vars 规则。

¥This rule will not report variables that are never read. Because it’s clearly an unused variable. If you want it reported, please enable the no-unused-vars rule.

在线运行
/* eslint no-useless-assignment: "error" */

function fn() {
    let v = 'unused';
    v = 'unused-2'
    doSomething();
}

已知限制

¥Known Limitations

当某些变量重新分配发生在 try 块内时,此规则不会报告它们。这是有意为之,因为由于潜在的提前退出或错误处理逻辑,此类分配仍可能在相应的 catch 块内或 try-catch 结构之后观察到。

¥This rule does not report certain variable reassignments when they occur inside the try block. This is intentional because such assignments may still be observed within the corresponding catch block or after the try-catch structure, due to potential early exits or error handling logic.

function foo() {
    let bar;
    try {
        bar = 2;
        unsafeFn();
        return { error: undefined };
    } catch {
        return { bar }; // `bar` is observed in the catch block
    }
}   
function unsafeFn() {
    throw new Error();
}

function foo() {
    let bar;
    try {
        bar = 2; // This assignment is relevant if unsafeFn() throws an error
        unsafeFn();
        bar = 4;
    } catch {
        // Error handling
    }
    return bar;
}   
function unsafeFn() {
    throw new Error();
}

何时不使用

¥When Not To Use It

如果你不想收到有关从未读取的值的通知,你可以安全地禁用此规则。

¥If you don’t want to be notified about values that are never read, you can safely disable this rule.

版本

此规则是在 ESLint v9.0.0-alpha.1 中引入。

进阶读物

资源