Skip to content

avoid_contradictory_expressions

v0.4.0 Warning Control Flow

Warns when a logical AND (&&) expression contains contradictory comparisons on the same variable, resulting in a condition that always evaluates to false. For example, x == 3 && x == 4 can never be true because x cannot equal both values simultaneously.

Contradictory conditions create unreachable code that silently does nothing. These are almost always bugs — typically from copy-paste errors where one operand was not updated, or from refactoring that accidentally introduced conflicting constraints. Catching them at analysis time prevents hard-to-debug logic errors.

See also: Effective Dart: Usage

void bad(int x, int y) {
// x cannot equal both 3 and 4
if (x == 3 && x == 4) {
print('unreachable');
}
// Impossible range — x can't be less than 4 AND greater than 4
if (x < 4 && x > 4) {
print('unreachable');
}
// Equality contradicts inequality
if (x == 2 && x != 2) {
print('unreachable');
}
// Same comparison with variable, opposite operators
if (x == y && x != y) {
print('unreachable');
}
}
void good(int x, int y) {
// Uses OR — x can be 3 or 4
if (x == 3 || x == 4) {
print('ok');
}
// Consistent range — x between 2 and 4
if (x < 4 && x > 2) {
print('ok');
}
// Different variables
if (x == 3 && y == 4) {
print('ok');
}
}

To disable this rule:

plugins:
many_lints:
diagnostics:
avoid_contradictory_expressions: false