Skip to content

eslint/no-self-assign Correctness

This rule is turned on by default.

What it does

Disallow assignments where both sides are exactly the same.

Why is this bad?

Self assignments have no effect, so probably those are an error due to incomplete refactoring. Those indicate that what you should do is still remaining.

Examples

Examples of incorrect code for this rule:

javascript
foo = foo;

[a, b] = [a, b];
[a, ...b] = [x, ...b];

({ a, b } = { a, x });

foo &&= foo;
foo ||= foo;
foo ??= foo;
javascript
obj.a = obj.a;
obj.a.b = obj.a.b;
obj["a"] = obj["a"];
obj[a] = obj[a];

Examples of correct code for this rule:

javascript
foo = bar;
[a, b] = [b, a];

// This pattern is warned by the `no-use-before-define` rule.
let foo = foo;

// The default values have an effect.
[foo = 1] = [foo];

// This ignores if there is a function call.
obj.a().b = obj.a().b;
a().b = a().b;

// `&=` and `|=` have an effect on non-integers.
foo &= foo;
foo |= foo;

Options

props

{ type: boolean, default: true }

The props option when set to false, disables the checking of properties.

With props set to false the following are examples of correct code:

javascript
obj.a = obj.a;
obj.a.b = obj.a.b;
obj["a"] = obj["a"];
obj[a] = obj[a];

How to use

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

bash
oxlint --deny no-self-assign
json
{
  "rules": {
    "no-self-assign": "error"
  }
}

References

Released under the MIT License.