parameter_ assignments
Don't reassign references to parameters of functions or methods.
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:
void badFunction(int parameter) { // LINT
parameter = 4;
}
BAD:
void badFunction(int required, {int optional: 42}) { // LINT
optional ??= 8;
}
BAD:
void badFunctionPositional(int required, [int optional = 42]) { // LINT
optional ??= 8;
}
BAD:
class A {
void badMethod(int parameter) { // LINT
parameter = 4;
}
}
GOOD:
void ok(String parameter) {
print(parameter);
}
GOOD:
void actuallyGood(int required, {int optional}) { // OK
optional ??= ...;
}
GOOD:
void actuallyGoodPositional(int required, [int optional]) { // OK
optional ??= ...;
}
GOOD:
class A {
void ok(String parameter) {
print(parameter);
}
}
Enable
#
To enable the
parameter_assignments
rule, add
parameter_assignments
under
linter > rules
in your
analysis_options.yaml
file:
linter:
rules:
- parameter_assignments
If you're instead using the YAML map syntax to configure linter rules,
add
parameter_assignments: true
under
linter > rules:
linter:
rules:
parameter_assignments: true
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.