Skip to main content

prefer-correct-mutated

effort: 2m
configurable
has IDE fix
has auto-fix
pro+

Warns when a parameter of the collection type (e.g. List or Set) is missing or has an unnecessary @mutated annotation.

@mutated is used to indicated parameters that are being mutated within the function/method body (e.g. .add(element)).

info

To use the @mutated annotation, install the dart-code-metrics-annotations package.

This annotation is used by avoid-mutating-parameters, avoid-collection-mutating-methods and avoid-not-assignable-collection-types rules.

⚙️ Config

Set additional-methods (default is empty) to add additional methods to the list of the default mutating methods.

analysis_options.yaml
dcm:
rules:
- prefer-correct-mutated:
additional-methods:
- customAddMethod

Example

❌ Bad:

// LINT: This parameter is mutated, but is not annotated with @mutated.
// Try adding the annotation.
void _extractConditionTargetsMap(Map<String?, String?> targets) {
targets['23'] = null;
}

// LINT: This parameter is mutated, but is not annotated with @mutated.
// Try adding the annotation.
void _extractConditionTargets(Set<String?> targets) {
targets.add(null);
}

// LINT: This parameter is annotated with @mutated, but is not mutated or passed to a @mutated argument.
// Try removing the annotation.
void _withAnnotation( Set<String?> targets) {
print(targets);
}

✅ Good:

void _extractConditionTargetsMap( Map<String?, String?> targets) {
targets['23'] = null;
}

void _extractConditionTargets( Set<String?> targets) {
targets.add(null);
}

void _withAnnotation(Set<String?> targets) {
print(targets);
}