avoid-bool-literals-in-conditional-expressions

Same as avoid_bool_literals_in_conditional_expressions in Dart lints

Group
Style
Path
lint/style/avoid-bool-literals-in-conditional-expressions
Default
On (recommended)
Origin
Dart lints
Rule
25 of 512
recommended

Flags conditional expressions over bool that have a bool literal branch.

When both branches of c ? a : b are non-nullable bools and one is a literal, the expression simplifies to a logical operator: c ? true : b is c || b, c ? false : b is !c && b, c ? b : true is !c || b, and c ? b : false is c && b. Branches are checked in their own flow state, after condition promotions. Reports once per conditional: the analyzer deduplicates upstream reports when both branches are literals at the same diagnostic location.

final ok = ready ? true : retry; // flagged
final ok = ready || retry;       // ok

Invalid

example.dartdart
void f(bool a, bool b, int i) {
  final x = a ? true : b;
  final y = a ? b : false;
  final z = a ? false : b;
  final w = a ? b : true;
  final v = i > 0 ? true : false;
  final u = a ? i > 2 : false;
  print([x, y, z, w, v, u]);
}

// Source-declared numeric getter contracts keep these promotion cases SDK-free.
class Number {
  bool get isInfinite => false;
  bool get isNaN => false;
}

bool infinite(dynamic number) =>
    number is Number ? number.isInfinite : false;

bool notANumber(dynamic number) =>
    number is Number ? number.isNaN : false;

bool booleanPromotion(Object? value) =>
    value is bool ? value : false;

bool inversePromotion(Object? value) =>
    value is! bool ? true : value;
Conditional expressions with a `bool` literal can be simplified.
1void f(bool a, bool b, int i) {
2 final x = a ? true : b;
Conditional expressions with a `bool` literal can be simplified.
2 final x = a ? true : b;
3 final y = a ? b : false;
Conditional expressions with a `bool` literal can be simplified.
3 final y = a ? b : false;
4 final z = a ? false : b;
Conditional expressions with a `bool` literal can be simplified.
4 final z = a ? false : b;
5 final w = a ? b : true;
Conditional expressions with a `bool` literal can be simplified.
5 final w = a ? b : true;
6 final v = i > 0 ? true : false;
Conditional expressions with a `bool` literal can be simplified.
6 final v = i > 0 ? true : false;
7 final u = a ? i > 2 : false;
Conditional expressions with a `bool` literal can be simplified.
17bool infinite(dynamic number) =>
18 number is Number ? number.isInfinite : false;
Conditional expressions with a `bool` literal can be simplified.
20bool notANumber(dynamic number) =>
21 number is Number ? number.isNaN : false;
Conditional expressions with a `bool` literal can be simplified.
23bool booleanPromotion(Object? value) =>
24 value is bool ? value : false;
Conditional expressions with a `bool` literal can be simplified.
26bool inversePromotion(Object? value) =>
27 value is! bool ? true : value;

Valid

example.dartdart
void f(bool a, bool b, bool? maybe, int i, dynamic d) {
  final x = a || b;
  final y = a ? b : !b;
  final z = a ? maybe : true;
  final w = a ? true : maybe;
  final v = a ? 1 : 2;
  final u = a ? d : false;
  final t = a ? 'yes' : 'no';
  print([x, y, z, w, v, u, t]);
}

Object? partialPromotion(Object? value) => value is bool ? true : value;

Object? inversePartialPromotion(Object? value) => value is! bool ? value : false;

Configure avoid-bool-literals-in-conditional-expressions

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-bool-literals-in-conditional-expressions": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-bool-literals-in-conditional-expressions: <reason>