parameter_assignments
Don't reassign references to parameters of functions or methods.
This rule is available as of Dart 2.0.
Details
#DON'T assign new values to parameters of methods or functions.
Assigning new values to parameters is generally a bad practice unless an
operator such as ??=
is used. Otherwise, arbitrarily reassigning parameters
is usually a mistake.
BAD:
dart
void badFunction(int parameter) { // LINT
parameter = 4;
}
BAD:
dart
void badFunction(int required, {int optional: 42}) { // LINT
optional ??= 8;
}
BAD:
dart
void badFunctionPositional(int required, [int optional = 42]) { // LINT
optional ??= 8;
}
BAD:
dart
class A {
void badMethod(int parameter) { // LINT
parameter = 4;
}
}
GOOD:
dart
void ok(String parameter) {
print(parameter);
}
GOOD:
dart
void actuallyGood(int required, {int optional}) { // OK
optional ??= ...;
}
GOOD:
dart
void actuallyGoodPositional(int required, [int optional]) { // OK
optional ??= ...;
}
GOOD:
dart
class A {
void ok(String parameter) {
print(parameter);
}
}
Usage
#To enable the parameter_assignments
rule,
add parameter_assignments
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- parameter_assignments
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。