Skip to content

foldable_negation

Reports a negation the language can fold, such as !(a == b). Write the folded form, such as a != b.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
writes the expression without the !

The violation is on the negation:

  • !!ready on a bool that cannot be null;
  • !(a == b) and !(a != b);
  • !(value is! T).

It does not report:

  • !(value is T), which the SDK lint prefer_is_not_operator (in recommended) reports;
  • !! on a dynamic value, whose first ! checks that it is a bool;
  • a negated comparison of order: !(ratio < 1) is not ratio >= 1 for NaN;
  • a negated && or ||.

The fix adds brackets where the place of the negation needs them.

Each pair means the same by the language itself. The folded form reads as what it means.

Violating
bool differ(int a, int b) => !(a == b);
Corrected
bool differ(int a, int b) => a != b;

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why it stays as written here.
// ignore: foldable_negation

// ignore_for_file: assay/foldable_negation silences it in a whole file, and // ignore: assay/foldable_negation with the prefix works too. See Suppressions.