avoid-redundant-else
- Group
- Style
- Path
lint/style/avoid-redundant-else- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 122 of 512
Flags else branches after an if body that unconditionally returns or throws.
The then branch must directly end in return, throw, or rethrow. Keep the else body's scope when moving it after the if. Identical branches belong to no-equal-then-else; a trailing bare return belongs to avoid-unnecessary-return instead, because removing that return is the smaller simplification. Only if statements directly inside blocks are checked, not nested unbraced branches or else-if links, where moving statements can change control flow.
Invalid
int returning(bool ready) {
if (ready) { return 1; }
else { print('other'); }
return 2;
}
void early(bool ready) {
if (ready) { return; }
else { print('other'); }
print('after');
}
int unbraced(bool ready) {
if (ready) return 1;
else return 2;
}
void throwing(bool ready) {
if (ready) { throw StateError('stop'); }
else { print('other'); }
}
void rethrowing(bool ready) {
try { throw StateError('stop'); } catch (error) {
if (ready) { rethrow; }
else { print(error); }
}
}
int comments(bool ready) {
if (ready) { return 1; /* explanation */ }
else { return 1; }
}
int exitingChain(bool first, bool second) {
if (first) return 1;
else if (second) return 2;
else return 3;
} if (ready) { return 1; } else { print('other'); } if (ready) { return; } else { print('other'); } if (ready) return 1; else return 2; if (ready) { throw StateError('stop'); } else { print('other'); } if (ready) { rethrow; } else { print(error); } if (ready) { return 1; /* explanation */ } else { return 1; } if (first) return 1; else if (second) return 2;Valid
void work(bool ready) {
if (ready) { print('first'); } else { print('second'); }
}
void trailing(bool ready) {
if (ready) { return; } else { print('second'); }
}
void conditional(bool ready, bool other) {
if (ready) { if (other) return; } else { print('second'); }
print('after');
}
int identical(bool ready) {
if (ready) { return 1; } else { return 1; }
}
void noElse(bool ready) {
if (ready) return;
print('after');
}
void loop(bool ready) {
while (ready) {
if (ready) { break; } else { print('second'); }
}
}
void chain(bool first, bool second) {
if (first) { print('first'); }
else if (second) { return; }
else { print('third'); }
print('after');
}
int nestedUnbraced(bool first, bool second) {
if (first) if (second) return 1; else return 2;
return 3;
}
void loopUnbraced(bool ready) {
while (ready) if (ready) return; else print('step');
}Configure avoid-redundant-else
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-redundant-else": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-redundant-else: <reason>More style rules
- avoid-redundant-pattern-field-names
avoid_redundant_pattern_field_namesrecommendedFlags redundant field names in object and record patterns.
- avoid-renaming-method-parameters
avoid_renaming_method_parametersRequires overriding methods to keep the positional parameter names of the member they inherit.
- avoid-return-types-on-setters
avoid_return_types_on_settersrecommendedFlags setters declared with an explicit return type.
- avoid-returning-this
avoid_returning_thisFlags methods that return
thisonly to enable a fluent interface. - avoid-setters-without-getters
avoid_setters_without_gettersFlags a setter that has no corresponding getter.
- avoid-single-cascade-in-expression-statements
avoid_single_cascade_in_expression_statementsrecommendedFlags an expression statement that is a cascade with a single section.
- avoid-single-child-column-or-row
avoid_single_child_column_or_rowflutterFlags a
Column,Row, orFlexwidget built with a single child. - avoid-top-level-member-access
Flags mutable global state: non-
const, non-finaltop-level variables and static fields, plus every read of mutable top-level variables.