avoid-unreachable-for-loop

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

example.dartdart
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); }); }
}
This condition requires the collection to be empty, so the iteration body cannot run.
1void listLoop(List<int> items) {
2 if (items.isEmpty) { for (final item in items) { print(item); } }
This condition requires the collection to be empty, so the iteration body cannot run.
4void listCallback(List<int> items) {
5 if (items.length == 0) { items.forEach(print); }
This condition requires the collection to be empty, so the iteration body cannot run.
7void reversedLength(List<int> items) {
8 if (0 == items.length) { for (final item in items) { print(item); } }
This condition requires the collection to be empty, so the iteration body cannot run.
10void negated(List<int> items) {
11 if (!items.isNotEmpty) { items.forEach(print); }
This condition requires the collection to be empty, so the iteration body cannot run.
13void setLoop(Set<String> items) {
14 if (items.isEmpty) { for (final item in items) { print(item); } }
This condition requires the collection to be empty, so the iteration body cannot run.
16void mapCallback(Map<String, int> items) {
17 if (items.isEmpty) { items.forEach((key, value) { print(value); }); }

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-unreachable-for-loop": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/avoid-unreachable-for-loop: <reason>