Skip to main content

avoid-negations-in-equality-checks

added in: 1.24.0
🛠
Pro+

Warns when an equality check has a negated expression.

Negated boolean expressions reduce readability and can sometimes be a mistake (e.g. == !value instead of != value).

Example

❌ Bad:

void fn(bool flag, bool another) {
// LINT: Avoid negated expressions in equality checks. Try removing this negation and inverting the operator.
if (!flag == another) {}

// LINT: Avoid negated expressions in equality checks. Try removing this negation and inverting the operator.
if (!flag != another) {}

// LINT: Avoid negated expressions in equality checks. Try removing this negation and inverting the operator.
if (flag == !another) {}

// LINT: Avoid negated expressions in equality checks. Try removing this negation and inverting the operator.
if (flag != !another) {}
}

✅ Good:

void fn(bool flag, bool another) {
if (flag != another) {}

if (flag == another) {}

if (flag != another) {}

if (flag == another) {}
}