prefer-iterable-every
Same as prefer_iterable_every in Pyramid Lint
- Group
- Complexity
- Path
lint/complexity/prefer-iterable-every- Default
- On (recommended)
- Origin
- Pyramid Lint
- Rule
- 380 of 512
Flags iterable.where(predicate).isEmpty and filtered-length comparisons.
An empty filtered iterable means every element fails the predicate. Prefer
iterable.every((element) => !predicate(element)) to express that condition
directly. When a filtered length equals the original iterable's length, use
iterable.every(predicate) instead. Both forms can stop at the first failure.
Only resolved SDK Iterable.where calls are checked. Length comparisons
require the same simple receiver or explicit this field on both sides.
Invalid
// Bad: filtered emptiness means every item fails the predicate.
void validateItems(List<int> items) {
if (items.where((x) => x > 0).isEmpty) {
print('No items are positive');
}
}
// Bad: .where().length == list.length pattern
void checkAllValid(List<String> values) {
bool allNonEmpty = values.where((v) => v.isNotEmpty).length == values.length;
if (allNonEmpty) {
print('All strings are non-empty');
}
}
// Bad: the predicate must be negated when replacing filtered emptiness.
void processCollection() {
final data = [2, 4, 6, 8];
final allOdd = data.where((n) => n % 2 == 0).isEmpty;
}
// Bad: nested where with length comparison
void checkMatrix(List<List<int>> matrix) {
if (matrix.where((row) => row.isNotEmpty).length == matrix.length) {
print('All rows are non-empty');
}
}
// Bad: where with complex predicate and length check
void checkAllPositive(List<int> numbers) {
final allPositive = numbers.where((n) => n > 0 && n < 100).length == numbers.length;
}
// Bad: filtered emptiness in a variable declaration.
void verifyList(List<String> items) {
bool allEmpty = items.where((i) => i.isNotEmpty).isEmpty;
}
// Regression: the violation must still be found inside Dart 3 containers
// (pattern declaration, labeled statement, switch expression and subject,
// collection if/spread, record field).
void containersRegression(int rcount, List<int> items) {
final (ra, _) = (items.where((x) => x > 1).isEmpty, 0);
lbl: {
final rb = items.where((x) => x > 1).isEmpty;
print(rb);
}
final rc = switch (rcount) {
0 => items.where((x) => x > 1).isEmpty,
_ => null,
};
final rd = switch (items.where((x) => x > 1).isEmpty) {
_ => 0,
};
final re = [if (rcount > 0) items.where((x) => x > 1).isEmpty];
final rf = [...[items.where((x) => x > 1).isEmpty]];
final rg = (p: items.where((x) => x > 1).isEmpty, q: 0);
print([ra, rc, rd, re, rf, rg]);
}
bool parenthesized(List<int> items) =>
(items.where((x) => x > 0).isEmpty);
void universalComparisons(List<int> items) {
final equalsTrue = items.where((x) => x > 0).isEmpty == true;
final differsFalse = items.where((x) => x > 0).isEmpty != false;
final reversedTrue = true == items.where((x) => x > 0).isEmpty;
final reversedFalse = false != (items.where((x) => x > 0).isEmpty);
final negatedComparison = !(items.where((x) => x > 0).isEmpty == false);
final repeatedComparison = (items.where((x) => x > 0).isEmpty == false) == false;
final doubleNegation = !(!items.where((x) => x > 0).isEmpty);
}
class CollectionOwner {
final List<int> items = [];
final List<int> others = [];
bool allPositive() =>
this.items.where((x) => x > 0).length == this.items.length;
bool unrelated() =>
this.items.where((x) => x > 0).length == this.others.length;
}
bool noEmptySet(Set<String> values) =>
values.where((value) => value.isEmpty).isEmpty;
bool noEvenIterable(Iterable<int> values) =>
values.where((value) => value.isEven).isEmpty;
bool noneMatch(List<int> values, bool Function(int) predicate) =>
values.where(predicate).isEmpty;void validateItems(List<int> items) { if (items.where((x) => x > 0).isEmpty) {void checkAllValid(List<String> values) { bool allNonEmpty = values.where((v) => v.isNotEmpty).length == values.length; final data = [2, 4, 6, 8]; final allOdd = data.where((n) => n % 2 == 0).isEmpty;void checkMatrix(List<List<int>> matrix) { if (matrix.where((row) => row.isNotEmpty).length == matrix.length) {void checkAllPositive(List<int> numbers) { final allPositive = numbers.where((n) => n > 0 && n < 100).length == numbers.length;void verifyList(List<String> items) { bool allEmpty = items.where((i) => i.isNotEmpty).isEmpty;void containersRegression(int rcount, List<int> items) { final (ra, _) = (items.where((x) => x > 1).isEmpty, 0); lbl: { final rb = items.where((x) => x > 1).isEmpty; final rc = switch (rcount) { 0 => items.where((x) => x > 1).isEmpty, }; final rd = switch (items.where((x) => x > 1).isEmpty) { }; final re = [if (rcount > 0) items.where((x) => x > 1).isEmpty]; final re = [if (rcount > 0) items.where((x) => x > 1).isEmpty]; final rf = [...[items.where((x) => x > 1).isEmpty]]; final rf = [...[items.where((x) => x > 1).isEmpty]]; final rg = (p: items.where((x) => x > 1).isEmpty, q: 0);bool parenthesized(List<int> items) => (items.where((x) => x > 0).isEmpty);void universalComparisons(List<int> items) { final equalsTrue = items.where((x) => x > 0).isEmpty == true; final equalsTrue = items.where((x) => x > 0).isEmpty == true; final differsFalse = items.where((x) => x > 0).isEmpty != false; final differsFalse = items.where((x) => x > 0).isEmpty != false; final reversedTrue = true == items.where((x) => x > 0).isEmpty; final reversedTrue = true == items.where((x) => x > 0).isEmpty; final reversedFalse = false != (items.where((x) => x > 0).isEmpty); final reversedFalse = false != (items.where((x) => x > 0).isEmpty); final negatedComparison = !(items.where((x) => x > 0).isEmpty == false); final negatedComparison = !(items.where((x) => x > 0).isEmpty == false); final repeatedComparison = (items.where((x) => x > 0).isEmpty == false) == false; final repeatedComparison = (items.where((x) => x > 0).isEmpty == false) == false; final doubleNegation = !(!items.where((x) => x > 0).isEmpty); bool allPositive() => this.items.where((x) => x > 0).length == this.items.length;bool noEmptySet(Set<String> values) => values.where((value) => value.isEmpty).isEmpty;bool noEvenIterable(Iterable<int> values) => values.where((value) => value.isEven).isEmpty;bool noneMatch(List<int> values, bool Function(int) predicate) => values.where(predicate).isEmpty;Valid
// Good: using .every() for a universal predicate.
void validateItems(List<int> items) {
if (items.every((x) => x > 0)) {
print('All items are positive');
}
}
// Good: .every() pattern for checking all elements
void checkAllValid(List<String> values) {
bool allNonEmpty = values.every((v) => v.isNotEmpty);
if (allNonEmpty) {
print('All strings are non-empty');
}
}
// Good: assignment with .every()
void processCollection() {
final data = [2, 4, 6, 8];
final allEven = data.every((n) => n % 2 == 0);
}
// Good: nested every() instead of where().length
void checkMatrix(List<List<int>> matrix) {
if (matrix.every((row) => row.isNotEmpty)) {
print('All rows are non-empty');
}
}
// Good: other iterable methods are fine
void useOtherMethods(List<int> numbers) {
final filtered = numbers.where((n) => n > 0).toList();
final hasAny = numbers.any((n) => n > 10);
}
// Negated filtered emptiness is any(predicate), not every(predicate).
void existentialChecks(List<int> items) {
final hasPositive = !items.where((x) => x > 0).isEmpty;
final parenthesized = !(items.where((x) => x > 0).isEmpty);
final nested = !((items.where((x) => x > 0).isEmpty));
final comparedFalse = items.where((x) => x > 0).isEmpty == false;
final comparedTrue = items.where((x) => x > 0).isEmpty != true;
final reversedFalse = false == (items.where((x) => x > 0).isEmpty);
final reversedTrue = true != ((items.where((x) => x > 0).isEmpty));
final negatedComparison = !(items.where((x) => x > 0).isEmpty == true);
final repeatedComparison = (items.where((x) => x > 0).isEmpty == false) != false;
final allNonPositive = items.every((x) => !(x > 0));
final empty = <int>[].every((x) => !(x > 0));
print([hasPositive, parenthesized, nested, allNonPositive, empty]);
}
class FakeString {
String where(bool Function(int) predicate) => '';
}
class FakeMap {
Map<int, int> where(bool Function(int) predicate) => {};
}
void unrelatedReceivers(
List<int> xs,
List<int> ys,
List<int> Function() source,
FakeString text,
FakeMap map,
) {
final textEmpty = text.where((x) => x > 0).isEmpty;
final mapEmpty = map.where((x) => x > 0).isEmpty;
final textLength = text.where((x) => x > 0).length == xs.length;
final mapLength = map.where((x) => x > 0).length == xs.length;
final unrelated = xs.where((x) => x > 0).length == ys.length;
final repeatedCall = source().where((x) => x > 0).length == source().length;
}
void nullableChains(List<int>? items, List<int> values) {
final nullableFilter = items?.where((x) => x > 0).isEmpty;
final nullableEmpty = values.where((x) => x > 0)?.isEmpty;
final nullableLength =
values.where((x) => x > 0)?.length == values.length;
final nullableOriginal =
values.where((x) => x > 0).length == values?.length;
}
bool unresolved(dynamic values) => values.where((value) => true).isEmpty;
class Lookalike {
Lookalike where(bool Function(int) predicate) => this;
bool get isEmpty => true;
}
bool lookalike(Lookalike values) => values.where((value) => true).isEmpty;Configure prefer-iterable-every
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"prefer-iterable-every": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/prefer-iterable-every: <reason>More complexity rules
- prefer-moving-to-variable
prefer-moving-to-variableFlags repeated property/getter chains in local variable initializers.
- prefer-null-aware-operators
prefer_null_aware_operatorsrecommendedFlags
x == null ? null : x.y(and the invertedx != null ? x.y : null). - unnecessary-const
unnecessary_constrecommendedFlags a redundant
constkeyword inside an already-const context. - unnecessary-getters-setters
unnecessary_getters_settersrecommendedFlags a trivial getter/setter pair that only reads and writes a private field.
- unnecessary-lambdas
unnecessary_lambdasrecommendedFlags a closure that only forwards its parameters to a function a tear-off could name.
- unnecessary-overrides
unnecessary_overridesrecommendedFlags a member that does nothing but forward to
superwith the same arguments and resolved contract. - widget-tree-depthflutter
Limits the depth of an inline Flutter widget construction tree.
- avoid-classes-with-only-static-members
avoid_classes_with_only_static_membersFlags a class that declares only static members.