Skip to main content

prefer_early_return

A rule which highlights if statements that span the entire body of a function or loop, and suggests replacing them with a reversed boolean check with an early return or continue.

Example config:​

solid_lints:
diagnostics:
prefer_early_return:
maximum_statements: 1
ignore_if_case: true

Example​

BAD:​

void func() {
if (a) { //LINT
c;
d;
}
}

void loop() {
for (final item in items) {
if (item.isValid) { //LINT
process(item);
save(item);
}
}
}

GOOD:​

void func() {
if (!a) return;
c;
d;
}

void loop() {
for (final item in items) {
if (!item.isValid) continue;
process(item);
save(item);
}
}

Parameters​

maximum_statements (int)​

The maximum number of statements allowed inside an if block before triggering the lint. If the number of statements does not exceed this threshold, the analysis is skipped.

ignore_if_case (bool)​

Whether to ignore if-case pattern matching statements.