目錄

control_flow_in_finally

避免在 finally 區塊中使用控制流程。

此規則從 Dart 2.0 開始提供。

規則集:推薦flutter

詳細資訊

#

避免控制流程離開 finally 區塊。

finally 區塊中使用控制流程,將不可避免地導致難以除錯的意外行為。

錯誤範例

dart
class BadReturn {
  double nonCompliantMethod() {
    try {
      return 1 / 0;
    } catch (e) {
      print(e);
    } finally {
      return 1.0; // LINT
    }
  }
}

錯誤範例

dart
class BadContinue {
  double nonCompliantMethod() {
    for (var o in [1, 2]) {
      try {
        print(o / 0);
      } catch (e) {
        print(e);
      } finally {
        continue; // LINT
      }
    }
    return 1.0;
  }
}

錯誤範例

dart
class BadBreak {
  double nonCompliantMethod() {
    for (var o in [1, 2]) {
      try {
        print(o / 0);
      } catch (e) {
        print(e);
      } finally {
        break; // LINT
      }
    }
    return 1.0;
  }
}

正確範例

dart
class Ok {
  double compliantMethod() {
    var i = 5;
    try {
      i = 1 / 0;
    } catch (e) {
      print(e); // OK
    }
    return i;
  }
}

用法

#

若要啟用 control_flow_in_finally 規則,請在 analysis_options.yaml 檔案中的 linter > rules 下新增 control_flow_in_finally

analysis_options.yaml
yaml
linter:
  rules:
    - control_flow_in_finally