where_tested_for_emptiness
Reports a where that is only tested with isEmpty or isNotEmpty. Use any or every instead.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- writes
any,everyor!any
What it reports
Section titled “What it reports”A where of an iterable that is only asked whether anything passed it: items.where(test).isNotEmpty and items.where(test).isEmpty. The violation covers the expression from the filtered value on.
Only a where and an isEmpty or isNotEmpty declared in the Dart libraries count, on a value whose static type is an Iterable, read with ..
It does not report:
- a
?.or a cascade in the chain; - a filter in brackets;
whereType;- a
whereof aStream, whoseisEmptyis a future; - a
whereof a class of your project.
The fix writes any for isNotEmpty. For isEmpty it writes every where the test is an arrow function that gives a !, and otherwise !items.any(test), in brackets where the place needs them. It is not offered where a comment stands in the expression.
any reads the elements in the same order and stops at the first that passes, as the emptiness test of the filter does, without making a lazy iterable on the way. And it asks the question directly.
Example
Section titled “Example”bool hasLarge(List<int> sizes) => sizes.where((size) => size > 9).isNotEmpty;bool allEven(List<int> sizes) => sizes.where((size) => !size.isEven).isEmpty;bool hasLarge(List<int> sizes) => sizes.any((size) => size > 9);bool allEven(List<int> sizes) => sizes.every((size) => size.isEven);Rule parameters
Section titled “Rule parameters”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. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// Why it stays as written here.// ignore: assay/where_tested_for_emptiness// ignore_for_file: assay/where_tested_for_emptiness silences it in a whole file, and // ignore: where_tested_for_emptiness without the prefix works too. See Suppressions.