Skip to content

duplicate_cascade_assignment

Reports a cascade section that sets a property to the value an earlier section already set. Make the copied section set the property that was meant.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
delete the repeated section

A section of a cascade that sets a property to the value an earlier section of the same cascade set it to, with nothing but assignments to other properties between the two: ..width = 1 ..height = 2 ..width = 1. The violation is at the section that repeats.

  • Both sections are plain assignments with =, and the same code: the same tokens in the same order, every name meaning the same declaration, as no_equal_branches compares branches. Whitespace and comments take no part.
  • The value must give the same value twice: a literal, a constant, a local variable, a parameter or a final field, or what the core operators make of them.

It does not report:

  • a section that calls a method, ..pop()..pop(), which does what it says twice;
  • an assignment repeated after a method section, ..width = 1 ..reset() ..width = 1, since the method can change what the property holds;
  • an assignment that sets the property back after another value, ..width = 1 ..width = 2 ..width = 1;
  • a value that a call, an index operator or a getter written in the source gives, or a collection created anew, which can be another value the second time;
  • a compound assignment, ..count += 1 ..count += 1.

A repeat through a setter written in the source is reported as well, also where the setter counts its calls.

A cascade that sets one property twice to the same value is a section that was copied to set another property and not changed. The property that was meant keeps its default.

The rule is advisory because the code runs; only the missing setting tells.

Violating
final paint = Paint()
..color = color
..strokeWidth = 2
..color = color;
Corrected
final paint = Paint()
..color = color
..strokeWidth = 2
..style = PaintingStyle.stroke;

The fix deletes the repeated section. It is offered where the property is a field, whose setter only stores the value, so the second assignment changes nothing. Where the property has a setter written in the source, the setter can do more, and the fix is not offered.

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.
assay:
rules:
duplicate_cascade_assignment:
exclude: ["lib/generated/**"]

Write why on the line directly above the suppression:

// The second write is what the test of the setter counts.
// ignore: assay/duplicate_cascade_assignment

// ignore_for_file: assay/duplicate_cascade_assignment silences it in a whole file, and // ignore: duplicate_cascade_assignment without the prefix works too. See Suppressions.