local_variable_only_copied
Reports a local variable whose one use is to initialize the variable declared right after it. Give the value to the next variable directly and delete this one.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- gives the value to the next variable and deletes the first
What it reports
Section titled “What it reports”A local variable whose one use is to be the whole initializer of the local variable declared by the very next statement: final response = fetch(); final result = response;. The violation is on the first variable.
It does not report:
- a variable read anywhere else, or written;
- a variable read as part of a larger value, returned, passed on, tested, or read in an assert, as one kept to look at in a debugger is;
- a statement between the two: the first variable then holds the value as it was before it;
- several variables in one declaration;
lateon either side.
The fix is offered only where neither declaration writes a type, or both write the same one, so that the value is inferred as before, and not where a comment stands before the first.
The second variable can take the value itself; the first only adds a name.
Example
Section titled “Example”final sum = values.fold(0, add);final result = sum;final result = values.fold(0, add);Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// Why it stays as written here.// ignore: local_variable_only_copied// ignore_for_file: assay/local_variable_only_copied silences it in a whole file, and // ignore: assay/local_variable_only_copied with the prefix works too. See Suppressions.