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.
Why use this rule
Section titled “Why use this rule”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();}Known limitations
Section titled “Known limitations”Reporting is restricted to comparisons where both operands are int, for two reasons:
- Doubles break the equivalence. With NaN involved,
!(a > b)anda <= bdiffer:!(double.nan > 1)istrue, whiledouble.nan <= 1isfalse. Rewriting would change behaviour, so doubles andnumare 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.
Configuration
Section titled “Configuration”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:
rules: avoid_inverted_boolean_checks: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_negated_conditions— State the positive case first in an if/else.avoid_unnecessary_negations— Collapse double negations.prefer_returning_condition— Return the condition instead of true/false branches.avoid_cascade_after_if_null— Detect cascades after if-null operators without parentheses.