Skip to content

avoid_constant_conditions

v0.4.0 Warning Control Flow

Warns when a binary comparison has constant operands on both sides, meaning the result is always the same at compile time. This usually indicates a typo, a copy-paste error, or dead code that should be cleaned up.

A condition like 4 == 11 or Config.channel == 'stable' (where Config.channel is a static const) always evaluates to the same boolean. One branch becomes unreachable dead code while the other always executes. This is almost never intentional and typically signals a mistake where one operand should have been a variable.

See also: Effective Dart: Usage

const _retryLimit = 4;
abstract final class Config {
static const channel = 'stable';
}
void bad() {
// Two integer literals compared
if (4 == 11) {
print('unreachable');
}
// Static const field compared to a string literal
if (Config.channel == 'stable') {
print('always true');
}
// Top-level const compared to a literal
final result = _retryLimit != 4;
// Boolean literals compared
final b = true == false;
}
void good(String value, int count) {
// Variable compared to literal
if (value == 'stable') {
print('hello');
}
// Variable compared to const
if (count > _retryLimit) {
print('big');
}
// Two variables
final a = count;
if (a == count) {
print('same');
}
}

This rule is in the core preset, so it is on with preset: core, preset: recommended or preset: opinionated.

To turn it off:

many_lints.yaml
rules:
avoid_constant_conditions: false

To keep the rule on but skip certain paths, use per-rule exclude.