duplicate_local_value
Reports a local variable given the same value, by the same code, as an earlier variable of the same block. Fix the copy, or use the first name.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- none
What it reports
Section titled “What it reports”A local variable given the value of an earlier variable of the same block, by the same code (the same tokens with the same meanings), where nothing between the two can change that value: final h = size.width; after final w = size.width;. The violation is at the later variable, and the message names the earlier one. Two variables of one declaration count as well.
- The value is repeatable, as assay’s equality rules read it: locals, parameters, final fields and constants, joined by the operators of the core library, and the members of the core library that only read (
length,isEmpty,first, …). It must read a variable. - Nothing between can change it. Neither variable, nor a local or a parameter the value reads, is assigned anywhere in the function. Where the value reads a member of the core library, which a call can change (
list.length), only declarations with repeatable values may stand between.
It does not report:
- values of literals and constants only:
var i = 0; var j = 0;; - a copy of one name:
final a = x;; latevariables;- a starting value that is assigned again, such as
var low = …; var high = …;in a search for the bounds; - a getter written in the source, an index, a call or a new object;
- a value read again after a call;
- variables of different blocks.
Two names for one value are either a slip, where the second meant another value (size.height), or one name too many. The rule cannot tell which, so it is advisory and has no fix.
Example
Section titled “Example”double area(Size size) { final width = size.width; final height = size.width; return width * height;}double area(Size size) { final width = size.width; final height = size.height; return width * height;}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:
// The two names say which side of the square each call means.// ignore: assay/duplicate_local_value// ignore_for_file: assay/duplicate_local_value silences it in a whole file, and // ignore: duplicate_local_value without the prefix works too. See Suppressions.