curly-braces-in-flow-control-structures
Same as curly_braces_in_flow_control_structures in Dart lints
- Group
- Style
- Path
lint/style/curly-braces-in-flow-control-structures- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 212 of 512
Requires curly braces around the bodies of flow-control statements.
A brace-less body invites the "goto fail" class of bug, where a later edit
adds a second statement that silently falls outside the branch. Requiring
blocks around for, while, do, and if/else bodies keeps the scope
explicit and edits safe. Two carve-outs match the official lint: an if
with no else whose body sits on the same line as its condition may omit
braces, and an else if chain need not wrap the intermediate if in a
block.
Invalid
void f1(bool x) {
while (x) doThing();
}
void f2() {
for (var i = 0; i < 3; i++) doThing();
}
void f3(bool x) {
do doThing();
while (x);
}
void f4(bool a) {
if (a)
doThing();
}
void f5(bool a) {
if (a)
doThing();
else
doOther();
}
void f6(bool a, List<int> xs) {
for (final x in xs) print(x);
}
// Closure argument of a constructor invocation (`Expr::New`).
Widget f7(bool x) {
return new ElevatedButton(onPressed: () {
while (x) doThing();
});
}
// Closure nested inside a collection literal.
List<VoidCallback> f8(bool x) {
return [
() {
if (x)
doThing();
},
];
}
void commentDelimiter(bool x) {
if (x)
/* ) */ doThing();
}void f1(bool x) { while (x) doThing();void f2() { for (var i = 0; i < 3; i++) doThing();void f3(bool x) { do doThing(); if (a) doThing(); if (a) doThing(); else doOther();void f6(bool a, List<int> xs) { for (final x in xs) print(x); return new ElevatedButton(onPressed: () { while (x) doThing(); if (x) doThing(); if (x) /* ) */ doThing();Valid
void g1(bool x) {
while (x) {
doThing();
}
}
void g2() {
for (var i = 0; i < 3; i++) {
doThing();
}
}
void g3(bool x) {
do {
doThing();
} while (x);
}
void g4(bool a) {
if (a) doThing();
}
void g5(bool a, bool b) {
if (a) {
doThing();
} else if (b) {
doOther();
} else {
doThird();
}
}
void g6(List<int> xs) {
for (final x in xs) {
print(x);
}
}Configure curly-braces-in-flow-control-structures
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"curly-braces-in-flow-control-structures": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/curly-braces-in-flow-control-structures: <reason>More style rules
- dangling-library-doc-comments
dangling_library_doc_commentsrecommendedAttach library documentation to an explicit
librarydirective. - deprecated-consistency
deprecated_consistencyrecommendedFlags deprecations that are not carried through to related declarations.
- directives-ordering
directives_orderingFlags import and export directives that break the Effective Dart ordering.
- do-not-use-environment
do_not_use_environmentDisallow reading compilation environment declarations, including type aliases.
- double-literal-format
double-literal-formatFlags double literals written with redundant or missing digits.
- empty-constructor-bodies
empty_constructor_bodiesrecommendedFlags constructors with an empty block body
{}. - enum-constants-ordering
Flags enum constants whose declared names violate the configured order.
- eol-at-end-of-file
eol_at_end_of_filerecommendedFlags a non-empty file that does not end with exactly one line break.