specify_nonobvious_local_variable_types
Specify non-obvious type annotations for local variables.
此规则目前处于 experimental 状态,自 Dart 3.6 版本起可用。
此规则提供 快速修复 。
_不兼容规则:omit_local_variable_types _
详情
#Do type annotate initialized local variables when the type is non-obvious.
Type annotations on local variables can serve as a request for type inference, documenting the expected outcome of the type inference step, and declaratively allowing the compiler and analyzer to solve the possibly complex task of finding type arguments and annotations in the initializing expression that yield the desired result.
Type annotations on local variables can also inform readers about the type of the initializing expression, which will allow them to proceed reading the subsequent lines of code with known good information about the type of the given variable (which may not be immediately evident by looking at the initializing expression).
An expression is considered to have a non-obvious type when it does not have an obvious type.
An expression e has an obvious type in the following cases:
- e is a non-collection literal. For instance, 1, true, 'Hello, $name!'.
- e is a collection literal with actual type arguments. For instance, <int, bool>{}.
- e is a list literal or a set literal where at least one element has an obvious type, and all elements have the same type. For instance, [1, 2] and { [true, false], [] }, but not [1, 1.5].
- e is a map literal where all key-value pair have a key with an obvious type and a value with an obvious type, and all keys have the same type, and all values have the same type. For instance, { #a:
[] }, but not {1: 1, 2: true}. - e is an instance creation expression whose class part is not raw. For instance C(14) if C is a non-generic class, or C
(14) if C accepts one type argument, but not C(14) if C accepts one or more type arguments. - e is a cascade whose target has an obvious type. For instance, 1..isEven..isEven has an obvious type because 1 has an obvious type.
- e is a type cast. For instance, myComplexExpression as int.
BAD:
List<List<Ingredient>> possibleDesserts(Set<Ingredient> pantry) {
var desserts = genericFunctionDeclaredFarAway(<num>[42], 'Something');
for (final recipe in cookbook) {
if (pantry.containsAll(recipe)) {
desserts.add(recipe);
}
}
return desserts;
}
const List<List<Ingredient>> cookbook = ...;
GOOD:
List<List<Ingredient>> possibleDesserts(Set<Ingredient> pantry) {
List<List<Ingredient>> desserts = genericFunctionDeclaredFarAway(
<num>[42],
'Something',
);
for (final List<Ingredient> recipe in cookbook) {
if (pantry.containsAll(recipe)) {
desserts.add(recipe);
}
}
return desserts;
}
const List<List<Ingredient>> cookbook = ...;
This rule is experimental. It is being evaluated, and it may be changed or removed. Feedback on its behavior is welcome! The main issue is here: https://github.com/dart-lang/linter/issues/3480.
使用方法
#要启用 specify_nonobvious_local_variable_types
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 specify_nonobvious_local_variable_types
:
linter:
rules:
- specify_nonobvious_local_variable_types
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.