Rules / Complexity / prefer-iterable-any

prefer-iterable-any

lint/complexity/prefer-iterable-any
recommended
Same as prefer_iterable_any · Pyramid Lint

Flags iterable.where(...).isNotEmpty.

Testing whether a filtered iterable is non-empty walks the filter just to check for any match; iterable.any(...) short-circuits on the first match and reads better. The rule matches an .isNotEmpty access on the result of a .where(...) call.

Invalid

example.dartdart
// Bad: using .where(predicate).isNotEmpty instead of .any()
void checkItems(List<int> items) {
  if (items.where((x) => x > 5).isNotEmpty) {
    print('Found item greater than 5');
  }
}

// Bad: .where().isNotEmpty pattern
void findValue(List<String> values) {
  bool hasEmpty = values.where((v) => v.isEmpty).isNotEmpty;
  if (hasEmpty) {
    print('Found empty string');
  }
}

// Bad: assignment with .where().isNotEmpty
void processCollection() {
  final data = [1, 2, 3, 4, 5];
  final hasLarge = data.where((n) => n > 3).isNotEmpty;
}

// Bad: nested where with isNotEmpty
void checkNested(List<List<int>> matrix) {
  if (matrix.where((row) => row.isNotEmpty).isNotEmpty) {
    print('Matrix has non-empty rows');
  }
}

// Bad: where with complex predicate
void filterByStatus(List<String> statuses) {
  final hasActive = statuses.where((s) => s == 'active' || s == 'pending').isNotEmpty;
}

// Bad: where on map/set
void checkMapKeys(Map<String, int> data) {
  if (data.keys.where((k) => k.startsWith('test')).isNotEmpty) {
    print('Has test keys');
  }
}
Use .any() instead of .where().isNotEmpty.
2void checkItems(List<int> items) {
3 if (items.where((x) => x > 5).isNotEmpty) {
Use .any() instead of .where().isNotEmpty.
9void findValue(List<String> values) {
10 bool hasEmpty = values.where((v) => v.isEmpty).isNotEmpty;
Use .any() instead of .where().isNotEmpty.
18 final data = [1, 2, 3, 4, 5];
19 final hasLarge = data.where((n) => n > 3).isNotEmpty;
Use .any() instead of .where().isNotEmpty.
23void checkNested(List<List<int>> matrix) {
24 if (matrix.where((row) => row.isNotEmpty).isNotEmpty) {
Use .any() instead of .where().isNotEmpty.
30void filterByStatus(List<String> statuses) {
31 final hasActive = statuses.where((s) => s == 'active' || s == 'pending').isNotEmpty;
Use .any() instead of .where().isNotEmpty.
35void checkMapKeys(Map<String, int> data) {
36 if (data.keys.where((k) => k.startsWith('test')).isNotEmpty) {

Valid

example.dartdart
// Good: using .any() instead of .where().isNotEmpty
void checkItems(List<int> items) {
  if (items.any((x) => x > 5)) {
    print('Found item greater than 5');
  }
}

// Good: .any() pattern for checking conditions
void findValue(List<String> values) {
  bool hasEmpty = values.any((v) => v.isEmpty);
  if (hasEmpty) {
    print('Found empty string');
  }
}

// Good: assignment with .any()
void processCollection() {
  final data = [1, 2, 3, 4, 5];
  final hasLarge = data.any((n) => n > 3);
}

// Good: nested any() instead of where().isNotEmpty
void checkNested(List<List<int>> matrix) {
  if (matrix.any((row) => row.isNotEmpty)) {
    print('Matrix has non-empty rows');
  }
}

// Good: other iterable methods are fine
void useOtherMethods(List<int> numbers) {
  final filtered = numbers.where((n) => n > 0).toList();
  final mapped = numbers.map((n) => n * 2);
}

How to configure

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

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