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 map 語法配置 linter 規則,請在 linter > rules 下新增 control_flow_in_finally: true
analysis_options.yaml
yaml
linter:
rules:
control_flow_in_finally: true