Skip to main content

avoid-contradictory-expressions

added in: 1.14.0
Pro+
preset: recommended

Warns when several conditions contradict each other.

Contradicting expressions always result in false which is undesired and is usually a sign of a bug or unused code.

Example

❌ Bad:

void fn() {
final num = 1;
final anotherNum = 2;

// LINT: This expression contradicts one of the previous expressions. Try updating this expression or replacing it with a different expression.
if (anotherNum == 3 && anotherNum == 4) {}

// LINT: This expression contradicts one of the previous expressions. Try updating this expression or replacing it with a different expression.
if (anotherNum < 4 && anotherNum > 4) {}

// LINT: This expression contradicts one of the previous expressions. Try updating this expression or replacing it with a different expression.
if (anotherNum == 2 && anotherNum != 2) {}

// LINT: This expression contradicts one of the previous expressions. Try updating this expression or replacing it with a different expression.
if (anotherNum == num && anotherNum != num) {}
}

✅ Good:

void fn() {
final num = 1;
final anotherNum = 2;

if (anotherNum == 3 || anotherNum == 4) {} // Correct, different operator
if (anotherNum < 4 && anotherNum > 2) {} // Correct, 2 instead of 4
if (anotherNum == 2) {} // Correct, only one comparison
if (anotherNum == num) {} // Correct, only one comparison
}

Additional Resources