Rules / Complexity / prefer-iterable-every

prefer-iterable-every

lint/complexity/prefer-iterable-every
recommended
Same as prefer_iterable_every · Pyramid Lint

Flags !iterable.where(...).isEmpty and iterable.where(...).length == other.length.

Both patterns ask whether every element satisfies a predicate; iterable.every(...) says so directly and short-circuits on the first failure. The rule matches a negated .where(...).isEmpty and a .where(...).length compared for equality against a .length.

Invalid

example.dartdart
// Bad: using !.where().isEmpty instead of .every()
void validateItems(List<int> items) {
  if (!items.where((x) => x > 0).isEmpty) {
    print('All 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: assignment with negated where isEmpty
void processCollection() {
  final data = [2, 4, 6, 8];
  final allEven = !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: negated where isEmpty in variable declaration
void verifyList(List<String> items) {
  bool hasContent = !items.where((i) => i.isNotEmpty).isEmpty;
}
Use .every() instead of .where().isEmpty or .where().length comparison.
2void validateItems(List<int> items) {
3 if (!items.where((x) => x > 0).isEmpty) {
Use .every() instead of .where().isEmpty or .where().length comparison.
9void checkAllValid(List<String> values) {
10 bool allNonEmpty = values.where((v) => v.isNotEmpty).length == values.length;
Use .every() instead of .where().isEmpty or .where().length comparison.
18 final data = [2, 4, 6, 8];
19 final allEven = !data.where((n) => n % 2 == 0).isEmpty;
Use .every() instead of .where().isEmpty or .where().length comparison.
23void checkMatrix(List<List<int>> matrix) {
24 if (matrix.where((row) => row.isNotEmpty).length == matrix.length) {
Use .every() instead of .where().isEmpty or .where().length comparison.
30void checkAllPositive(List<int> numbers) {
31 final allPositive = numbers.where((n) => n > 0 && n < 100).length == numbers.length;
Use .every() instead of .where().isEmpty or .where().length comparison.
35void verifyList(List<String> items) {
36 bool hasContent = !items.where((i) => i.isNotEmpty).isEmpty;

Valid

example.dartdart
// Good: using .every() instead of !.where().isEmpty
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);
}

How to configure

Set the severity of prefer-iterable-every in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "prefer-iterable-every": "error"
      }
    }
  }
}