control_flow_in_finally
避免在 finally
區塊中使用控制流程。
詳細資訊
#避免 控制流程離開 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
如果您改為使用 YAML 地圖語法來配置 linter 規則,請在 linter > rules 下新增 control_flow_in_finally: true
analysis_options.yaml
yaml
linter:
rules:
control_flow_in_finally: true
除非另有說明,否則本網站上的文件反映的是 Dart 3.7.1 版本。頁面最後更新於 2025-03-07。 檢視原始碼 或 回報此頁面的問題。