Skip to content

avoid_inverted_boolean_checks

v0.8.0 Warning Fix Control Flow

This rule flags a negated relational comparison on integers — !(a > b), !(a <= b) — where the opposite operator says the same thing directly.

Every relational operator has an exact opposite. Negating the comparison instead of using that opposite forces the reader to invert the condition mentally, which is a small cost that recurs at every read.

if (!(count > limit)) {
accept();
}
if (count <= limit) {
accept();
}

Reporting is restricted to comparisons where both operands are int, for two reasons:

  • Doubles break the equivalence. With NaN involved, !(a > b) and a <= b differ: !(double.nan > 1) is true, while double.nan <= 1 is false. Rewriting would change behaviour, so doubles and num are never reported.
  • User-defined operators need not be consistent. A type may define > and <= independently, so the opposite operator is not guaranteed to be the negation.

Equality (!(a == b)) is out of scope here; see avoid_unnecessary_negations for the double-negation cases.

This rule is in the recommended preset, so it is on with preset: recommended or preset: opinionated. Add it to preset: core with avoid_inverted_boolean_checks: true.

To turn it off:

many_lints.yaml
rules:
avoid_inverted_boolean_checks: false

To keep the rule on but skip certain paths, use per-rule exclude.