prefer-iterable-any

Same as prefer_iterable_any in Pyramid Lint

Path
lint/complexity/prefer-iterable-any
Default
On (recommended)
Origin
Pyramid Lint
Rule
379 of 512
recommended

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');
  }
}

// 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).isNotEmpty, 0);
  lbl: {
    final rb = items.where((x) => x > 1).isNotEmpty;
    print(rb);
  }
  final rc = switch (rcount) {
    0 => items.where((x) => x > 1).isNotEmpty,
    _ => null,
  };
  final rd = switch (items.where((x) => x > 1).isNotEmpty) {
    _ => 0,
  };
  final re = [if (rcount > 0) items.where((x) => x > 1).isNotEmpty];
  final rf = [...[items.where((x) => x > 1).isNotEmpty]];
  final rg = (p: items.where((x) => x > 1).isNotEmpty, q: 0);
  print([ra, rc, rd, re, rf, rg]);
}
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) {
Use `.any()` instead of `.where().isNotEmpty`.
44void containersRegression(int rcount, List<int> items) {
45 final (ra, _) = (items.where((x) => x > 1).isNotEmpty, 0);
Use `.any()` instead of `.where().isNotEmpty`.
46 lbl: {
47 final rb = items.where((x) => x > 1).isNotEmpty;
Use `.any()` instead of `.where().isNotEmpty`.
50 final rc = switch (rcount) {
51 0 => items.where((x) => x > 1).isNotEmpty,
Use `.any()` instead of `.where().isNotEmpty`.
53 };
54 final rd = switch (items.where((x) => x > 1).isNotEmpty) {
Use `.any()` instead of `.where().isNotEmpty`.
56 };
57 final re = [if (rcount > 0) items.where((x) => x > 1).isNotEmpty];
Use `.any()` instead of `.where().isNotEmpty`.
57 final re = [if (rcount > 0) items.where((x) => x > 1).isNotEmpty];
58 final rf = [...[items.where((x) => x > 1).isNotEmpty]];
Use `.any()` instead of `.where().isNotEmpty`.
58 final rf = [...[items.where((x) => x > 1).isNotEmpty]];
59 final rg = (p: items.where((x) => x > 1).isNotEmpty, q: 0);

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);
}

Configure prefer-iterable-any

Set its severity in falcon.json. See the configuration guide.

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

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/complexity/prefer-iterable-any: <reason>
  • prefer-iterable-everyprefer_iterable_everyrecommended

    Flags iterable.where(predicate).isEmpty and filtered-length comparisons.

  • prefer-moving-to-variableprefer-moving-to-variable

    Flags repeated property/getter chains in local variable initializers.

  • prefer-null-aware-operatorsprefer_null_aware_operatorsrecommended

    Flags x == null ? null : x.y (and the inverted x != null ? x.y : null).

  • unnecessary-constunnecessary_constrecommended

    Flags a redundant const keyword inside an already-const context.

  • unnecessary-getters-settersunnecessary_getters_settersrecommended

    Flags a trivial getter/setter pair that only reads and writes a private field.

  • unnecessary-lambdasunnecessary_lambdasrecommended

    Flags a closure that only forwards its parameters to a function a tear-off could name.

  • unnecessary-overridesunnecessary_overridesrecommended

    Flags a member that does nothing but forward to super with the same arguments and resolved contract.

  • widget-tree-depthflutter

    Limits the depth of an inline Flutter widget construction tree.