prefer-early-return
- Group
- Style
- Path
lint/style/prefer-early-return- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 349 of 512
Flags a trailing if that wraps multiple statements in a void function.
Invert the condition and return early to remove a level of indentation.
Only a function body's final statement is considered, with no else, a
plain expression condition, and a block with multiple statements or a nested
if. Explicitly void functions, methods, operators and local functions,
setters, and constructors are checked. Non-void and inferred-return functions,
closures, generators, pattern conditions, and single-statement guard clauses
are left alone: a bare return must not change a value-returning contract.
Invalid
void top(bool ready) {
if (ready) {
print('ready');
print('work');
}
}
void afterWork(bool ready) {
print('start');
if (ready) {
print('ready');
print('work');
}
}
class Worker {
Worker(bool ready) {
if (ready) {
print('ready');
print('work');
}
}
void run(bool ready) {
if (ready) {
print('ready');
print('work');
}
}
set ready(bool value) {
if (value) {
print('ready');
print('work');
}
}
}
void outer() {
void local(bool ready) {
if (ready) {
print('ready');
print('work');
}
}
local(true);
}
void nested(bool first, bool second) {
if (first) {
if (second) { print('one'); print('two'); }
}
}
set ready(bool value) {
if (value) {
print('one'); print('two');
}
}
void asynchronous(bool value) async {
if (value) {
print('one'); print('two');
}
}
class Indexed {
void operator []=(int index, bool value) {
if (value) {
print(index); print(value);
}
}
}void top(bool ready) { if (ready) { print('start'); if (ready) { Worker(bool ready) { if (ready) { void run(bool ready) { if (ready) { set ready(bool value) { if (value) { void local(bool ready) { if (ready) {void nested(bool first, bool second) { if (first) {set ready(bool value) { if (value) {void asynchronous(bool value) async { if (value) { void operator []=(int index, bool value) { if (value) {Valid
void guard(bool ready) {
if (!ready) return;
print('ready');
print('work');
}
void single(bool ready) {
if (ready) { print('one'); }
}
void withElse(bool ready) {
if (ready) { print('one'); print('two'); }
else { print('other'); }
}
void notLast(bool ready) {
if (ready) { print('one'); print('two'); }
print('end');
}
int value(bool ready) {
if (ready) { print('one'); return 1; }
return 0;
}
void pattern(Object value) {
if (value case int number) { print(number); print('int'); }
}
void empty(bool ready) { if (ready) {} }
void expression(bool ready) { if (ready) print('one'); }
void inferred(bool value) {
final callback = () {
if (value) { print('one'); print('two'); }
};
callback();
}
dynamic inferredReturn(bool value) {
if (value) { print('one'); return 1; }
}
Future<void> future(bool value) async {
if (value) { print('one'); print('two'); }
}
Iterable<int> generator(bool value) sync* {
if (value) { yield 1; yield 2; }
}
class Value {
int method(bool value) {
if (value) { print('one'); return 1; }
return 0;
}
}Configure prefer-early-return
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-early-return": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-early-return: <reason>More style rules
- prefer-equatable-mixinequatable
Prefer
EquatableMixinto directly extending package:equatable's Equatable. - prefer-final-fields
prefer_final_fieldsrecommendedFlags a private field that is initialized but never reassigned, so it could be
final(prefer-final-fields, adopted from package:lints). - prefer-final-in-for-each
prefer_final_in_for_eachrecommendedPrefer
finalfor a for-each loop variable that is never reassigned. - prefer-final-locals
prefer_final_localsrecommendedPrefer
finalfor local variable declarations that are never reassigned. - prefer-final-parameters
prefer_final_parametersPrefer
finalfor parameters that are never reassigned. - prefer-first
prefer-firstFlags
[0]index access that should use the.firstgetter. - prefer-for-in
Flags zero-based, unit-step index loops over a syntactically known list.
- prefer-for-loop-in-childrenflutter
Prefers collection-for elements over eager map/toList chains in Flutter children.