prefer-iterable-any
Same as prefer_iterable_any in Pyramid Lint
- Group
- Complexity
- Path
lint/complexity/prefer-iterable-any- Default
- On (recommended)
- Origin
- Pyramid Lint
- Rule
- 379 of 512
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
// 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]);
}void checkItems(List<int> items) { if (items.where((x) => x > 5).isNotEmpty) {void findValue(List<String> values) { bool hasEmpty = values.where((v) => v.isEmpty).isNotEmpty; final data = [1, 2, 3, 4, 5]; final hasLarge = data.where((n) => n > 3).isNotEmpty;void checkNested(List<List<int>> matrix) { if (matrix.where((row) => row.isNotEmpty).isNotEmpty) {void filterByStatus(List<String> statuses) { final hasActive = statuses.where((s) => s == 'active' || s == 'pending').isNotEmpty;void checkMapKeys(Map<String, int> data) { if (data.keys.where((k) => k.startsWith('test')).isNotEmpty) {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; final rc = switch (rcount) { 0 => items.where((x) => x > 1).isNotEmpty, }; final rd = switch (items.where((x) => x > 1).isNotEmpty) { }; final re = [if (rcount > 0) items.where((x) => x > 1).isNotEmpty]; final re = [if (rcount > 0) items.where((x) => x > 1).isNotEmpty]; final rf = [...[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);Valid
// 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.
{
"linter": {
"rules": {
"complexity": {
"prefer-iterable-any": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/prefer-iterable-any: <reason>More complexity rules
- prefer-iterable-every
prefer_iterable_everyrecommendedFlags
iterable.where(predicate).isEmptyand filtered-length comparisons. - 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.