Skip to main content

avoid-unconditional-break

added in: 1.9.0
Pro+
preset: recommended

Warns when a break, continue, return or throw are used unconditionally in a for loop.

Example

❌ Bad:

void main() {
final list = <int>[];

for (final item in list) {
continue; // LINT: Avoid unconditional break statements. This loop is always interrupted after one iteration. Try removing this statement or call it conditionally.
}

for (final item in list) {
break; // LINT: Avoid unconditional break statements. This loop is always interrupted after one iteration. Try removing this statement or call it conditionally.
}

for (final item in list) {
return; // LINT: Avoid unconditional break statements. This loop is always interrupted after one iteration. Try removing this statement or call it conditionally.
}

for (final item in list) {
throw ''; // LINT: Avoid unconditional break statements. This loop is always interrupted after one iteration. Try removing this statement or call it conditionally.
}
}

✅ Good:

void main() {
final list = <int>[];

for (final item in list) {
if (someCondition) { // Correct, only in some cases
continue;
}
}

for (final item in list) {
if (someCondition) {
break;
}
}

for (final item in list) {
if (someCondition) {
return;
}
}

for (final item in list) {
if (someCondition) {
throw '';
}
}
}

Additional Resources