avoid-unnecessary-return
- Group
- Style
- Path
lint/style/avoid-unnecessary-return- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 171 of 512
Flags bare returns at the end of a function or its final if/block branches.
Falling through already exits the function. Only paths through final block statements and if branches are followed; loops, switch, try/finally, and value-returning statements are deliberately excluded. Nested functions have their own boundary, so their returns cannot escape into an enclosing body. Identical if/else branches are left to no-equal-then-else.
Invalid
void direct() {
print('work');
return;
}
void branch(bool ready) {
if (ready) {
return;
}
}
void branches(bool ready) {
if (ready) {
return;
} else {
print('work');
return;
}
}
class Example {
void method() {
return;
}
set value(int value) {
print(value);
return;
}
}
void closures() {
final callback = () {
return;
};
callback();
}
void local() {
void nested() {
return;
}
nested();
}
class Constructed {
Constructed() {
return;
}
} print('work'); return; if (ready) { return; if (ready) { return; print('work'); return; void method() { return; print(value); return; final callback = () { return; void nested() { return; Constructed() { return;Valid
void guard(bool ready) {
if (ready) return;
print('work');
}
int value() { return 1; }
void loop(bool ready) {
while (ready) { return; }
}
void counted() {
for (var i = 0; i < 3; i++) { return; }
}
void handling() {
try { return; } finally { print('cleanup'); }
}
void selection(int value) {
switch (value) {
case 1: return;
default: print('other');
}
}
void nested(bool ready) {
if (ready) {
if (ready) return;
print('work');
}
}
void identical(bool ready) {
if (ready) { return; } else { return; }
}
void identicalWork(bool ready) {
if (ready) { print('work'); return; }
else { print('work'); return; }
}Configure avoid-unnecessary-return
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-unnecessary-return": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-unnecessary-return: <reason>More style rules
- avoid-unnecessary-super
Omit implicit
super()initializers and provably redundant member prefixes. - avoid-unused-constructor-parameters
avoid_unused_constructor_parametersFlags constructor parameters that are never used.
- avoid-void-async
avoid_void_asyncrecommendedAvoid
asyncfunctions and methods declared to returnvoid. - avoid-wrapping-in-paddingflutter
Flags Padding directly wrapping a Flutter Container that can own the padding.
- binary-expression-operand-order
binary-expression-operand-orderFlags comparison expressions with a literal on the left-hand side.
- boolean-prefixes
boolean_prefixesFlags boolean identifiers that lack a conventional interrogative prefix.
- camel-case-extensions
camel_case_extensionsrecommendedFlags named extensions whose name is not
UpperCamelCase. - camel-case-types
camel_case_typesrecommendedFlags type names that are not
UpperCamelCase.