avoid-mutating-parameters
- Group
- Suspicious
- Path
lint/suspicious/avoid-mutating-parameters- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 95 of 512
Detects writes through named function, method and constructor parameter objects.
Closure parameters are exempt; parameter rebinding belongs to parameter-assignments.
Parameters annotated @mutated (dart-code-metrics-annotations) are exempt.
Invalid
class Box { int value = 0; Box? child; }
void mutate(Box box, List<int> values, Map<String, int> map) {
box.value = 1;
box.value += 1;
++box.value;
box.value--;
values[0] = 2;
map['key'] ??= 3;
box.child!.value = 2;
box..value = 4;
values..[0] = 5;
void nested() { box.value = 6; }
final closure = () { values[0]++; };
nested(); closure();
}
class Holder {
set item(Box value) { value.value = 1; }
}
class Mutator {
Mutator(Box box) {
box.value = 1;
}
void update(Box box) {
box.value = 2;
}
void operator []=(int index, Box box) {
box.value = index;
}
}
void outer() {
void local(Box box) {
box.value = 3;
}
local(Box());
}
void castReceivers(Object value, Box? box) {
(value as List<int>)[0] = 1;
(value as Box).value = 2;
box!.value = 3;
(box! as Box).value++;
}void mutate(Box box, List<int> values, Map<String, int> map) { box.value = 1; box.value = 1; box.value += 1; box.value += 1; ++box.value; ++box.value; box.value--; box.value--; values[0] = 2; values[0] = 2; map['key'] ??= 3; map['key'] ??= 3; box.child!.value = 2; box.child!.value = 2; box..value = 4; box..value = 4; values..[0] = 5; values..[0] = 5; void nested() { box.value = 6; } void nested() { box.value = 6; } final closure = () { values[0]++; };class Holder { set item(Box value) { value.value = 1; } Mutator(Box box) { box.value = 1; void update(Box box) { box.value = 2; void operator []=(int index, Box box) { box.value = index; void local(Box box) { box.value = 3;void castReceivers(Object value, Box? box) { (value as List<int>)[0] = 1; (value as List<int>)[0] = 1; (value as Box).value = 2; (value as Box).value = 2; box!.value = 3; box!.value = 3; (box! as Box).value++;Valid
class Box { int value = 0; }
void noMutation(Box box, List<int> values) {
final local = Box();
local.value = 1;
final copy = [...values];
copy[0] = 2;
final read = box.value;
box = Box(); // Rebinding belongs to parameter-assignments.
{
final box = Box();
box.value = read;
}
void nested() { final box = Box(); box.value++; }
final callback = () { final values = [0]; values[0]++; };
nested(); callback();
}
class Owner {
Box box;
Owner(this.box) { box.value = 1; }
}
const mutated = 0;
void intentional(@mutated Box box, @mutated List<int> values) {
box.value = 1;
values[0]++;
}
class AnnotatedSetter {
set item(@mutated Box value) { value.value++; }
}
class Builder { String name = ''; }
class Options { final headers = <String, String>{}; }
class Item { bool selected = true; }
void closures(List<Item> items) {
final build = (Builder b) => b..name = 'x';
final intercept = (Options options) {
options.headers['Authorization'] = 'token';
};
items.forEach((i) => i.selected = false);
build(Builder());
intercept(Options());
}Configure avoid-mutating-parameters
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"suspicious": {
"avoid-mutating-parameters": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/avoid-mutating-parameters: <reason>More suspicious rules
- avoid-negated-conditions
Prefer positive conditions when both branches can be swapped.
- avoid-nested-assignments
Require assignments to be standalone statements, arrow bodies or loop operations.
- avoid-non-final-exception-class-fields
Exception and Error payloads should not change after construction.
- avoid-passing-async-when-sync-expected
avoid-passing-async-when-sync-expectedrecommendedFlags an
asyncclosure passed to a proven synchronous callback parameter. - avoid-passing-bloc-to-blocbloc
Rejects direct Bloc and Cubit arguments to other blocs.
- avoid-passing-bloc-to-widgetbloc
Rejects direct Bloc and Cubit arguments to Flutter widget constructors.
- avoid-passing-build-context-to-blocsbloc
Keeps Flutter
BuildContextarguments outside Bloc and Cubit APIs. - avoid-print
avoid_printrecommendedflutterFlags calls and callback arguments resolving to
dart:core'sprint.