Skip to main content

avoid-self-compare

added in: 1.1.0
Free+
preset: recommended

Warns when a comparison has both sides exactly the same.

Example

❌ Bad:

void main() {
final val = '1';

// LINT: Both sides of this comparison are equal. Try checking it for a potential mistake.
if (val == val) {
return;
}

// LINT: Both sides of this comparison are equal. Try checking it for a potential mistake.
if (val.length == val.length) {
return;
}

// LINT: Both sides of this comparison are equal. Try checking it for a potential mistake.
if (val > val) {
return;
}

// LINT: Both sides of this comparison are equal. Try checking it for a potential mistake.
if (val < val) {
return;
}
}

✅ Good:

void main() {
final val = '1';
final anotherVal = '2';

if (val == anotherVal) { // Correct, another variable
return;
}

if (val.length == anotherVal.length) {
return;
}

if (val > anotherVal) {
return;
}

if (val < anotherVal) {
return;
}
}

Additional Resources