Skip to main content

avoid-unnecessary-negations

has auto-fix
pro+

Warns when a negation can be simplified.

Example

❌ Bad:

void main() {
final someCondition = true;

// LINT: Avoid unnecessary negations. Try rewriting the code without it.
if (!!someCondition) {}

// LINT: Avoid unnecessary negations. Try rewriting the code without it.
if (!true) {}

// LINT: Avoid unnecessary negations on each side of this binary expression. Try removing them.
if (!flag == !another) {}

// LINT: Avoid unnecessary negations on each side of this binary expression. Try removing them.
if (!flag != !another) {}
}

✅ Good:

void main() {
final someCondition = true;

if (someCondition) {} // Correct, uses 'someCondition' directly

if (false) {} // Correct, uses 'false' instead

if (flag == another) {}
}