Skip to main content

avoid-equal-expressions

added in: 1.3.0
🛠
Pro+
preset: recommended

Warns when both sides of a binary expression are the same.

Example

❌ Bad:

// LINT: Avoid duplicate expressions. Try removing the duplicate expression or replacing it with a different expression.
if (_isOneShifting() && _isOneShifting()) {
return;
}

// LINT: Avoid duplicate expressions. Try removing the duplicate expression or replacing it with a different expression.
if (num == anotherNum && num == anotherNum) {
return;
}

// LINT: Avoid duplicate expressions. Try removing the duplicate expression or replacing it with a different expression.
if (num == anotherNum || num == anotherNum) {
return;
}

final val = num << num; // LINT: Avoid duplicate expressions. Try removing the duplicate expression or replacing it with a different expression.
final val = num >> num; // LINT: Avoid duplicate expressions. Try removing the duplicate expression or replacing it with a different expression.
final val = 5 / 5; // LINT: Avoid duplicate expressions. Try removing the duplicate expression or replacing it with a different expression.
final val = 10 - 10; // LINT: Avoid duplicate expressions. Try removing the duplicate expression or replacing it with a different expression.

✅ Good:

if (_isOneShifting() && _anotherOne()) { // Correct, different invocation
return;
}

if (num == anotherNum) {
return;
}

if (num == anotherNum) {
return;
}

final val = num << anotherNum;
final val = num >> anotherNum;

Additional Resources