目录

avoid_redundant_argument_values

Avoid redundant argument values.

此规则自 Dart 2.8 版本起可用。

此规则提供 快速修复

详情

#

DON'T pass an argument that matches the corresponding parameter's default value.

Note that a method override can change the default value of a parameter, so that an argument may be equal to one default value, and not the other. Take, for example, two classes, A and B where B is a subclass of A, and B overrides a method declared on A, and that method has a parameter with one default value in A's declaration, and a different default value in B's declaration. If the static type of the target of the invoked method is B, and B's default value matches the argument, then the argument can be omitted (and if the argument value is different, then a lint is not reported). If, however, the static type of the target of the invoked method is A, then a lint may be reported, but we cannot know statically which method is invoked, so the reported lint may be a false positive. Such cases can be ignored inline with a comment like // ignore: avoid_redundant_argument_values.

BAD:

dart
void f({bool valWithDefault = true, bool? val}) {
  ...
}

void main() {
  f(valWithDefault: true);
}

GOOD:

dart
void f({bool valWithDefault = true, bool? val}) {
  ...
}

void main() {
  f(valWithDefault: false);
  f();
}

使用方法

#

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

analysis_options.yaml
yaml
linter:
  rules:
    - avoid_redundant_argument_values