no-literal-bool-comparisons

Same as no_literal_bool_comparisons in Dart lints

Group
Style
Path
lint/style/no-literal-bool-comparisons
Default
Off, opt in
Origin
Dart lints
Rule
290 of 512

Flags boolean literals compared or combined with a non-nullable bool.

x == true is just x, x != true is !x, x && true is x, and so on for ||, &, |, and ^. The literal is only redundant when the other operand is provably a non-nullable bool; a bool? compared to true is a meaningful null check and is left alone. Parenthesized literals are separate expressions and are not reported.

if (done == true) {} // flagged
if (done) {}         // ok

Invalid

example.dartdart
void f(bool a, bool b, bool? maybe) {
  if (a == true) {}
  if (false != a) {}
  if (a && true) {}
  if (false || b) {}
  final c = a & true;
  final d = b ^ false;
  final e = (a == b) == true;
  print([c, d, e, maybe]);
}

bool ready() => true;

void g(Object o) {
  if (ready() == false) {}
  if ((o is String) != true) {}
}
Remove the unnecessary comparison to a boolean literal.
1void f(bool a, bool b, bool? maybe) {
2 if (a == true) {}
Remove the unnecessary comparison to a boolean literal.
2 if (a == true) {}
3 if (false != a) {}
Remove the unnecessary comparison to a boolean literal.
3 if (false != a) {}
4 if (a && true) {}
Remove the unnecessary comparison to a boolean literal.
4 if (a && true) {}
5 if (false || b) {}
Remove the unnecessary comparison to a boolean literal.
5 if (false || b) {}
6 final c = a & true;
Remove the unnecessary comparison to a boolean literal.
6 final c = a & true;
7 final d = b ^ false;
Remove the unnecessary comparison to a boolean literal.
7 final d = b ^ false;
8 final e = (a == b) == true;
Remove the unnecessary comparison to a boolean literal.
14void g(Object o) {
15 if (ready() == false) {}
Remove the unnecessary comparison to a boolean literal.
15 if (ready() == false) {}
16 if ((o is String) != true) {}

Valid

example.dartdart
void f(bool a, bool? maybe, dynamic d, Object o, int i) {
  if (a) {}
  if (!a) {}
  if (maybe == true) {}
  if (maybe != false) {}
  if (d == true) {}
  if (o == true) {}
  if (i == 1) {}
  if (a == maybe) {}
  final x = maybe ?? false;
  print(x);
}

Configure no-literal-bool-comparisons

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "no-literal-bool-comparisons": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/no-literal-bool-comparisons: <reason>
  • no-magic-numberno-magic-number

    Flags unnamed numeric literals (magic numbers) used inline in expressions.

  • no-object-declarationno-object-declaration

    Flags class members declared with the type Object (or Object?).

  • no-runtime-type-to-stringno_runtimeType_toString

    Avoid calling toString() on runtimeType.

  • non-constant-identifier-namesnon_constant_identifier_namesrecommended

    Flags non-constant identifiers that are not written in lowerCamelCase.

  • omit-local-variable-typesomit_local_variable_types

    Flags local variable type annotations that repeat the initializer's type.

  • only-throw-errorsonly_throw_errorsrecommended

    Only throw instances of classes that extend Exception or Error.

  • package-namespackage_namesrecommendedcross-file

    Require package names to be valid lower_case_with_underscores identifiers.

  • parameter-assignmentsparameter_assignments

    Don't reassign parameters of functions, methods, or constructors.