avoid-bitwise-operators-with-booleans

Path
lint/suspicious/avoid-bitwise-operators-with-booleans
Default
Off, opt in
Origin
Falcon
Rule
22 of 512

Flags & and | applied to two non-nullable boolean operands.

Prefer && and || to express logical intent and short-circuit evaluation. Both operands must resolve to core bool; integer, dynamic, nullable, and unresolved operands are left alone. Exclusive-or and compound assignments are not replacements for logical conjunction or disjunction.

Invalid

example.dartdart
void example(bool left, bool right) {
  final a = left & right;
  final b = left | right;
  final c = true & false;
  final local = true;
  final d = local | right;
  final e = (left == right) & (a != b);
  final f = (!left) | (!right);
}
Use `&&` instead of a bitwise operator on booleans.
1void example(bool left, bool right) {
2 final a = left & right;
Use `||` instead of a bitwise operator on booleans.
2 final a = left & right;
3 final b = left | right;
Use `&&` instead of a bitwise operator on booleans.
3 final b = left | right;
4 final c = true & false;
Use `||` instead of a bitwise operator on booleans.
5 final local = true;
6 final d = local | right;
Use `&&` instead of a bitwise operator on booleans.
6 final d = local | right;
7 final e = (left == right) & (a != b);
Use `||` instead of a bitwise operator on booleans.
7 final e = (left == right) & (a != b);
8 final f = (!left) | (!right);

Valid

example.dartdart
void example(bool left, bool right, int bits, dynamic unknown) {
  final a = left && right;
  final b = left || right;
  final c = bits & 3;
  final d = bits | 4;
  final e = left ^ right;
  final f = unknown & right;
  left &= right;
  left |= right;
}

Configure avoid-bitwise-operators-with-booleans

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-bitwise-operators-with-booleans": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/suspicious/avoid-bitwise-operators-with-booleans: <reason>