use-if-null-to-convert-nulls-to-bools

Same as use_if_null_to_convert_nulls_to_bools in Dart lints

Group
Style
Path
lint/style/use-if-null-to-convert-nulls-to-bools
Default
On (recommended)
Origin
Dart lints
Rule
492 of 512
recommended

Prefers ?? to comparing a nullable bool against a boolean literal.

For a bool? value b, b == true is b ?? false and b != false is b ?? true; the if-null forms state the null default directly. The rule fires on == with true and != with false on the right when the left operand's static type is proven to be bool?.

Operand types include promotions proven at the comparison's use site. Parenthesized boolean literals do not match the official rule.

Invalid

example.dartdart
bool? maybe() => null;

class Flags {
  bool? enabled;
}

void check(bool? flag, Flags flags) {
  if (flag == true) {}
  if (flag != false) {}
  print(maybe() == true);
  print(flags.enabled != false);
  final bool? local = flag;
  print(local == true);
}

Future<bool?> answer() async => null;

Future<void> awaited() async {
  print((await answer()) == true);
  final result = await answer();
  print(result != false);
}
Use an if-null operator to convert a `null` to a `bool`.
7void check(bool? flag, Flags flags) {
8 if (flag == true) {}
Use an if-null operator to convert a `null` to a `bool`.
8 if (flag == true) {}
9 if (flag != false) {}
Use an if-null operator to convert a `null` to a `bool`.
9 if (flag != false) {}
10 print(maybe() == true);
Use an if-null operator to convert a `null` to a `bool`.
10 print(maybe() == true);
11 print(flags.enabled != false);
Use an if-null operator to convert a `null` to a `bool`.
12 final bool? local = flag;
13 print(local == true);
Use an if-null operator to convert a `null` to a `bool`.
18Future<void> awaited() async {
19 print((await answer()) == true);
Use an if-null operator to convert a `null` to a `bool`.
20 final result = await answer();
21 print(result != false);

Valid

example.dartdart
void check(bool? flag, bool strict, Object? other) {
  if (flag ?? false) {}
  if (flag ?? true) {}
  print(flag == false);
  print(flag != true);
  print(strict == true);
  print(true == flag);
  print(other == true);
  print(flag == null);
}

void promoted(bool? early) {
  if (early == null) return;
  if (early == true) print(1);
}

Configure use-if-null-to-convert-nulls-to-bools

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "use-if-null-to-convert-nulls-to-bools": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/use-if-null-to-convert-nulls-to-bools: <reason>
  • use-late-for-private-fields-and-variablesuse_late_for_private_fields_and_variables

    Flags a private nullable field or top-level variable that is only ever assigned non-null values and read through !, so it could be late with a non-nullable type.

  • use-named-constantsuse_named_constantsrecommended

    Prefers a class's predefined static constant to constructing an equal constant.

  • use-raw-stringsuse_raw_strings

    Flags string literals whose only escapes are \\ and \$, which a raw string would express without any backslash.

  • use-setters-to-change-propertiesuse_setters_to_change_properties

    Flags one-parameter void methods that only assign that parameter to a field.

  • use-spacer-as-expanded-childuse_spacer_as_expanded_childrecommendedflutter

    Flags an Expanded wrapping an empty SizedBox or Container that should be a Spacer.

  • use-string-in-part-of-directivesuse_string_in_part_of_directivesrecommended

    Flags a part of directive that names its library instead of using a string URI.

  • use-super-parametersuse_super_parametersrecommended

    Prefer super parameters when a constructor only forwards parameters to its superclass.

  • use-test-throws-matchersuse_test_throws_matchersrecommended

    Prefers throwsA matchers over try { ...; fail(...); } catch (...) {}.