sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-unused-reassignment`

Closed

#991 opened on Jan 2, 2021

 (3 comments) (3 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Prevent accidentally reassigning a variable and not using the result. It would only handle variables and reassignments in the same scope.

This is similar to no-unused-vars, but that rule doesn't handle this case.

It cannot auto-fix as getUnicorn() could be impure. We should at least provide a suggestion.

Fail

function foo() {
	let unicorn = getUnicorn();

	if (unicorn) {
		handleUnicorn(unicorn);
	}

	// …

	unicorn = getUnicorn();
}
let foo = 1;

// …

foo = 2;

// …

foo = 3;

console.log(foo);

Pass

function foo() {
	let unicorn = getUnicorn();

	if (unicorn) {
		handleUnicorn(unicorn);
	}
}
let foo = 1;

console.log(foo);

Contributor guide