Skip to main content

avoid-conditions-with-boolean-literals

added in: 1.16.0
Pro+
preset: recommended

Warns then a binary expression has a boolean constant that either makes the resulting value always the same or does not affect it.

Example

❌ Bad:

void fn() {
final someSet = {1, 2, 3};

// LINT: This literal makes the condition to always result in 'false'.
final value = someSet.contains(1) && false;

// LINT: This literal has no effect on the condition.
final value = someSet.contains(1) && true;

// LINT: This literal makes the condition to always result in 'true'.
final value = someSet.contains(1) || true;

// LINT: This literal has no effect on the condition.
final value = someSet.contains(1) || false;
}

✅ Good:

void fn() {
final someSet = {1, 2, 3};

final value = someSet.contains(1); // Correct, no redundant literals
final value = someSet.contains(1) && _someOtherFlag; // Correct, no redundant literals
}

Additional Resources