avoid-unnecessary-continue

Group
Style
Path
lint/style/avoid-unnecessary-continue
Default
Off, opt in
Origin
Falcon
Rule
160 of 512

Flags an unlabeled continue at the end of a loop body or its final branch.

Ancestor inspection stops at try/finally, switch and function boundaries.

Invalid

example.dartdart
void classic() {
  for (var i = 0; i < 3; i++) {
    print(i);
    continue;
  }
}

void iterable(List<int> values) {
  for (final value in values) {
    print(value);
    continue;
  }
}

void waiting(bool ready) {
  while (ready) {
    continue;
  }
}

void once() {
  do {
    continue;
  } while (false);
}

Future<void> asynchronous(Stream<int> values) async {
  await for (final value in values) {
    print(value);
    continue;
  }
}

void unbraced(bool ready) {
  while (ready) continue;
}

void conditionalTail(List<int> values) {
  for (final value in values) {
    if (value < 0) {
      continue;
    } else {
      print(value);
      continue;
    }
  }
}
Remove this trailing `continue`; the loop already advances here.
3 print(i);
4 continue;
Remove this trailing `continue`; the loop already advances here.
10 print(value);
11 continue;
Remove this trailing `continue`; the loop already advances here.
16 while (ready) {
17 continue;
Remove this trailing `continue`; the loop already advances here.
22 do {
23 continue;
Remove this trailing `continue`; the loop already advances here.
29 print(value);
30 continue;
Remove this trailing `continue`; the loop already advances here.
34void unbraced(bool ready) {
35 while (ready) continue;
Remove this trailing `continue`; the loop already advances here.
40 if (value < 0) {
41 continue;
Remove this trailing `continue`; the loop already advances here.
43 print(value);
44 continue;

Valid

example.dartdart
void guarded(List<int> values) {
  for (final value in values) {
    if (value < 0) continue;
    print(value);
  }
}

void labeled(List<int> values) {
  outer: for (final first in values) {
    for (final second in values) {
      print(first + second);
      continue outer;
    }
  }
}

void conditionalWork(List<int> values) {
  for (final value in values) {
    if (value < 0) { continue; }
    print(value);
  }
}

void finallyTail(bool ready) {
  while (ready) {
    try { continue; } finally { break; }
  }
}

void laterWork(bool ready) {
  while (ready) {
    { continue; }
    print('later');
  }
}

void noContinue() {
  for (var i = 0; i < 3; i++) { print(i); }
  do { print('once'); } while (false);
}

Configure avoid-unnecessary-continue

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-unnecessary-continue": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-unnecessary-continue: <reason>