literal-only-boolean-expressions

Same as literal_only_boolean_expressions in Dart lints

Path
lint/correctness/literal-only-boolean-expressions
Default
On (recommended)
Origin
Dart lints
Rule
257 of 512
recommended

Flags conditions built only from literals, whose value never changes.

An if, while, do-while, or for condition (or a pattern when guard) made solely of literals combined with operators always evaluates the same way, so the branch is either dead or unconditional. while (true) is allowed as the idiomatic infinite loop. Guard diagnostics include the when keyword.

if (1 > 2) {}          // flagged
while (true && false) {} // flagged
while (true) {}        // ok
if (x > 2) {}          // ok

Invalid

example.dartdart
void bad(int x) {
  if (true) {}
  if (1 > 2) {}
  if (!false) {}
  if ('a' == 'b' || null == null) {}
  while (true && false) {}
  do {} while (false);
  for (var i = 0; 1 < 2; i++) {}
  if (1 is int) {}
  if (null ?? x > 1) {}
  switch (x) {
    case 1 when true:
      print(x);
    default:
  }
  final y = switch (x) {
    1 when 2 > 1 => 'a',
    _ => 'b',
  };
  print(y);
}

void constantInterpolation() {
  const a = 20;
  if ('$a' == '20') {}
  if ('${1 + 2}' == '3') {}
  const flag = true;
  if ('${flag ? a : 0}' == '20') {}
}
The Boolean expression has a constant value.
1void bad(int x) {
2 if (true) {}
The Boolean expression has a constant value.
2 if (true) {}
3 if (1 > 2) {}
The Boolean expression has a constant value.
3 if (1 > 2) {}
4 if (!false) {}
The Boolean expression has a constant value.
4 if (!false) {}
5 if ('a' == 'b' || null == null) {}
The Boolean expression has a constant value.
5 if ('a' == 'b' || null == null) {}
6 while (true && false) {}
The Boolean expression has a constant value.
6 while (true && false) {}
7 do {} while (false);
The Boolean expression has a constant value.
7 do {} while (false);
8 for (var i = 0; 1 < 2; i++) {}
The Boolean expression has a constant value.
8 for (var i = 0; 1 < 2; i++) {}
9 if (1 is int) {}
The Boolean expression has a constant value.
9 if (1 is int) {}
10 if (null ?? x > 1) {}
The Boolean expression has a constant value.
11 switch (x) {
12 case 1 when true:
The Boolean expression has a constant value.
16 final y = switch (x) {
17 1 when 2 > 1 => 'a',
The Boolean expression has a constant value.
24 const a = 20;
25 if ('$a' == '20') {}
The Boolean expression has a constant value.
25 if ('$a' == '20') {}
26 if ('${1 + 2}' == '3') {}
The Boolean expression has a constant value.
27 const flag = true;
28 if ('${flag ? a : 0}' == '20') {}

Valid

example.dartdart
void good<T>(int x, bool flag, String s) {
  if (x > 2) {}
  if (flag) {}
  if (flag && true) {}
  while (true) {}
}

void more<T>(int x, String s) {
  do {} while (x > 0);
  for (var i = 0; i < 2; i++) {}
  for (;;) {
    break;
  }
  for (final e in [1, 2]) {
    print(e);
  }
  if (1 is T) {}
  if ('$x' == '1') {}
  if (x case 1) {}
  switch (x) {
    case 1 when x > 0:
      print(x);
    default:
  }
  final list = [if (x > 1) 1];
  print(list);
}

void nonConstantInterpolation() {
  final a = 20;
  if ('$a' == '20') {}
  const outer = 20;
  void shadowed(int outer) {
    if ('$outer' == '20') {}
  }
  shadowed(outer);
}

Configure literal-only-boolean-expressions

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "literal-only-boolean-expressions": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/literal-only-boolean-expressions: <reason>
  • match-getter-setter-field-names

    Detect likely copy-paste errors in trivial accessors when a different field matching the accessor name is declared.

  • missing-test-assertionrecommendedtest

    Reports inline test callbacks that contain no recognized assertion calls.

  • no-adjacent-strings-in-listno_adjacent_strings_in_listrecommended

    Flags adjacent string literals used as an element of a list or set literal.

  • no-import-cyclescross-file

    Report mutually dependent components of imported or exported library files.

  • no-logic-in-create-stateno_logic_in_create_staterecommendedflutter

    Require StatefulWidget.createState to return only a fresh zero-argument Flutter State construction.

  • no-self-assignmentsno_self_assignmentsrecommended

    Flags assignments of a variable to itself, such as x = x;.

  • null-check-on-nullable-type-parameternull_check_on_nullable_type_parameterrecommended

    Flags a null assertion x! on a nullable type parameter T? whose expected type is T or T? for a potentially nullable T, and null-assert patterns whose matched value is a nullable type parameter.

  • null-closuresnull_closuresrecommended

    Reject literal null in the closure slots of curated SDK APIs.