curly_braces_in_flow_control_structures

Stable
Core
Fix available

DO use curly braces for all flow control structures.

Details

#

DO use curly braces for all flow control structures.

Doing so avoids the dangling else problem.

BAD:

dart
if (overflowChars != other.overflowChars)
  return overflowChars < other.overflowChars;

GOOD:

dart
if (isWeekDay) {
  print('Bike to work!');
} else {
  print('Go dancing or read a book!');
}

There is one exception to this: an if statement with no else clause where the entire if statement (including the condition and the body) fits in one line. In that case, you may leave off the braces if you prefer:

GOOD:

dart
if (arg == null) return defaultValue;

If the body wraps to the next line, though, use braces:

GOOD:

dart
if (overflowChars != other.overflowChars) {
  return overflowChars < other.overflowChars;
}

Enable

#

To enable the curly_braces_in_flow_control_structures rule, add curly_braces_in_flow_control_structures under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:
  rules:
    - curly_braces_in_flow_control_structures

If you're instead using the YAML map syntax to configure linter rules, add curly_braces_in_flow_control_structures: true under linter > rules:

analysis_options.yaml
yaml
linter:
  rules:
    curly_braces_in_flow_control_structures: true