no-self-assign
Disallow assigning a variable or a property to itself
warn by default
name = name does nothing. It's usually left over from a refactor, or a typo for this.name = name.
The rule checks =, &&=, ||= and ??=, and looks inside destructuring. count += count isn't reported, because it changes the value.
This rule is on by default.
Configure#
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"no-self-assign" = "off" # or "error"Every option, set to its default:
bunfig.toml
[lint.rules]
"no-self-assign" = ["warn", {
props = true,
}]Examples#
example.js3 problems
function rename(user, name) {
name = namename is assigned to itself.;
user.name = user.nameuser.name is assigned to itself.;
[user, name] = [useruser is assigned to itself., "guest"];
}example.jsNo problems
function rename(user, name) {
user.name = name;
[user, name] = [name, user];
}Options#
| Option | Default | What it does |
|---|---|---|
props | true | Also report properties, such as user.name = user.name. |
Turn props off if your code assigns a property to itself to run its setter:
example.jswith
{ "props": false }No problemsvideo.src = video.src; // loads the video again