avoid-unreachable-for-loop
- Group
- Correctness
- Path
lint/correctness/avoid-unreachable-for-loop- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 177 of 512
Reports iteration of a collection in a branch that requires it to be empty.
Only resolved local SDK collections and immediate iteration are inspected. Fields/getters, intervening statements and unknown collection types may change between the guard and iteration, so they are deliberately left alone.
Invalid
void listLoop(List<int> items) {
if (items.isEmpty) { for (final item in items) { print(item); } }
}
void listCallback(List<int> items) {
if (items.length == 0) { items.forEach(print); }
}
void reversedLength(List<int> items) {
if (0 == items.length) { for (final item in items) { print(item); } }
}
void negated(List<int> items) {
if (!items.isNotEmpty) { items.forEach(print); }
}
void setLoop(Set<String> items) {
if (items.isEmpty) { for (final item in items) { print(item); } }
}
void mapCallback(Map<String, int> items) {
if (items.isEmpty) { items.forEach((key, value) { print(value); }); }
}void listLoop(List<int> items) { if (items.isEmpty) { for (final item in items) { print(item); } }void listCallback(List<int> items) { if (items.length == 0) { items.forEach(print); }void reversedLength(List<int> items) { if (0 == items.length) { for (final item in items) { print(item); } }void negated(List<int> items) { if (!items.isNotEmpty) { items.forEach(print); }void setLoop(Set<String> items) { if (items.isEmpty) { for (final item in items) { print(item); } }void mapCallback(Map<String, int> items) { if (items.isEmpty) { items.forEach((key, value) { print(value); }); }Valid
void valid(List<int> items, List<int> others) {
for (final item in items) { print(item); }
if (items.isNotEmpty) { items.forEach(print); }
if (items.length != 0) { for (final item in items) { print(item); } }
if (items.isEmpty) { for (final item in others) { print(item); } }
// Mutation between guard and loop invalidates the empty fact.
if (items.isEmpty) { items.add(1); for (final item in items) { print(item); } }
if (items.isEmpty) { final items = others; items.forEach(print); }
if (items.isEmpty) { print('empty'); } else { items.forEach(print); }
}
class Custom {
bool get isEmpty => true;
void forEach(void Function(int) action) { action(1); }
}
void custom(Custom items) {
if (items.isEmpty) { items.forEach(print); }
}Configure avoid-unreachable-for-loop
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-unreachable-for-loop": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-unreachable-for-loop: <reason>More correctness rules
- avoid-unrelated-type-castsrecommended
Flags casts between types proven unrelated by canonical type resolution.
- avoid-unsafe-collection-methods
Flags potentially throwing element access on SDK collections.
- avoid-unsafe-reduce
Warn when SDK Iterable.reduce can receive an empty collection.
- avoid-unused-after-null-check
Reports a null-checked local ignored by a branch that passes another local of the same resolved type to a call.
- avoid-unused-parameters
avoid-unused-parametersrecommendedDisallow declared parameters that are never used, excluding primary constructors.
- avoid-watch-outside-buildrecommendedprovider
Flags listening Provider accesses proven to run outside Flutter builds.
- avoid-web-libraries-in-flutter
avoid_web_libraries_in_flutterrecommendedflutterDisallow importing web-only
dart:libraries from Flutter code. - await-only-futures
await_only_futuresrecommendedAwait only values assignable to futures;
FutureOr, dynamic, bottom types and extension types are accepted like the analyzer.