Skip to main content

no-boolean-literal-compare

added in: 1.6.0
⚙️🛠
Free+

Warns on comparison to a boolean literal, as in x == true.

Comparing boolean values to boolean literals is unnecessary, as those expressions will result in booleans too. Just use the boolean values directly or negate them.

⚙️ Config

Set allow-false (default is false) to allow value == false or false == value checks (example).

analysis_options.yaml
dart_code_metrics:
rules:
- no-boolean-literal-compare:
allow-false: false

Example

❌ Bad:

var b = x == true; // LINT: Comparing boolean values to boolean literals is unnecessary, as those expressions will also result in boolean values. Try using the boolean values directly or negating them.
var c = x != true; // LINT: Comparing boolean values to boolean literals is unnecessary, as those expressions will also result in boolean values. Try using the boolean values directly or negating them.

// LINT: Comparing boolean values to boolean literals is unnecessary, as those expressions will also result in boolean values. Try using the boolean values directly or negating them.
if (x == true) {
...
}

// LINT: Comparing boolean values to boolean literals is unnecessary, as those expressions will also result in boolean values. Try using the boolean values directly or negating them.
if (x != false) {
...
}

✅ Good:

var b = x;
var c = !x;

if (x) {
...
}

if (!x) {
...
}

Example with "allow-false"

Config
analysis_options.yaml
dart_code_metrics:
rules:
- no-boolean-literal-compare:
allow-false: true

✅ Good:

void fn(bool? flag) {
// Correct, allowed
if (flag == false) {
...
}
}