跳到主要内容

curly_braces_in_flow_control_structures

稳定版
核心
修复可用

请务必为所有流程控制结构使用花括号。

详情

#

请务必 为所有流程控制结构使用花括号。

这样做可以避免 悬空的 else 问题。

不良

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

良好

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

对此有一个例外:对于没有 else 子句的 if 语句,如果整个 if 语句(包括条件和主体)可以放在一行中。在这种情况下,如果您愿意,可以省略花括号

良好

dart
if (arg == null) return defaultValue;

但是,如果主体换行到下一行,请使用花括号

良好

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

启用

#

要启用 curly_braces_in_flow_control_structures 规则,请在您的 analysis_options.yaml 文件中的 linter > rules 下添加 curly_braces_in_flow_control_structures

analysis_options.yaml
yaml
linter:
  rules:
    - curly_braces_in_flow_control_structures

如果您改为使用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 curly_braces_in_flow_control_structures: true

analysis_options.yaml
yaml
linter:
  rules:
    curly_braces_in_flow_control_structures: true