avoid-unnecessary-block
- Group
- Style
- Path
lint/style/avoid-unnecessary-block- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 155 of 512
Flags standalone nested blocks that introduce no lexical declarations.
Control-flow and function bodies retain their braces. A block containing a variable, pattern, or local function declaration retains its lexical scope.
Invalid
void expressions() {
{ print('nested'); }
}
void empty() {
{}
}
int returning() {
{ return 1; }
}
void multiple() {
{ print('first'); print('second'); }
}
void conditional(bool ready) {
{ if (ready) { print('ready'); } }
}
void scopedChild() {
{
{ final value = 1; print(value); }
}
}void expressions() { { print('nested'); }void empty() { {}int returning() { { return 1; }void multiple() { { print('first'); print('second'); }void conditional(bool ready) { { if (ready) { print('ready'); } }void scopedChild() { {Valid
void variables() {
final value = 1;
{ final value = 2; print(value); }
print(value);
}
void patterns() {
{ var (first, second) = (1, 2); print(first + second); }
}
void functions() {
{ void local() {} local(); }
}
void controlBodies(bool ready) {
if (ready) { print('ready'); } else { print('waiting'); }
while (ready) { break; }
for (var i = 0; i < 2; i++) { print(i); }
do { print('once'); } while (false);
}
void exceptionBodies() {
try { print('try'); } catch (error) { print(error); } finally { print('finally'); }
}
void labels() {
exit: { print('labeled'); break exit; }
}
void laterDeclaration() {
{ print('before'); final value = 1; print(value); }
}Configure avoid-unnecessary-block
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-unnecessary-block": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-unnecessary-block: <reason>More style rules
- avoid-unnecessary-call
Prefer direct invocation of functions and resolved callable instances.
- avoid-unnecessary-continue
Flags an unlabeled continue at the end of a loop body or its final branch.
- avoid-unnecessary-getter
Flags a getter that only exposes a private final field.
- avoid-unnecessary-if
Flags an if branch that only returns the same expression as the following return.
- avoid-unnecessary-length-check
Empty collections already skip
for-inandforEachbodies. - avoid-unnecessary-local-variable
Reports a single-use local used only to initialize the next local.
- avoid-unnecessary-negations
Simplifies negated bool literals and equality comparisons with a logical negation on both sides.
- avoid-unnecessary-overrides-in-stateflutter
Redundant Flutter State overrides that drop inherited
@protected.