Skip to content

duplicated_code

Reports a stretch of shipped code that is copied elsewhere in the project. Move the shared code into one function, widget or class, and use it from each place.

Kind
cross-file check
Tier
advisory
Host
command-line program only (assay cross-file)
Fix
none

A duplicated region: a run of lines in one file under lib/ that copies of a block of code found elsewhere in the project cover. The message counts the region’s lines and names the other copies:

This block of 29 lines is duplicated at lib/screens/home.dart:4-32.
This block of 29 lines is duplicated at lib/screens/detail.dart:4-32 and lib/screens/tally.dart:5-23.
This block of 29 lines is duplicated at lib/copies/c02.dart:4-32, lib/copies/c03.dart:4-32, lib/copies/c04.dart:4-32 and 8 more places.
  • A block is a run of at least 100 tokens (min_tokens) found at two or more places, each spanning at least 10 lines (min_lines).
  • The check compares the analyzer’s tokens, not text. Comments and layout do not count, and changed labels and sizes still make a copy.
  • Names stay as written, so a copy whose variables or widgets were renamed is not found.
  • Each copy is a result of its own that names the others: two copies of a widget in two screens are two results.
  • Only the shipped code is read: the Dart files under lib/ of each package. Files that look generated, or do not compile, are left out.

When code is copied, a change lands in one place and not in the other: a fix reaches the screen it was found in but not its copy, a new field shows in the list but not in the detail. An agent copies readily, since the quickest way to a second screen is to paste the first and rename it, and nothing in one file shows it.

The check is advisory, because some copies are kept on purpose: two screens meant to drift apart, code generated by hand once, a legacy screen kept until it is removed. Each project decides.

Set under the assay: key of the project’s root analysis_options.yaml. The check is on under every preset unless you switch it off.

Key Default Effect
assay.cross_file.duplicated_code.enabled true false switches the check off, and the pass then reads no token. The short form duplicated_code: false means the same.
assay.cross_file.duplicated_code.exclude [] Path globs, from the project root, whose files the check does not read.
assay.cross_file.duplicated_code.min_tokens 100 The fewest tokens a block has: a whole number, 20 or more.
assay.cross_file.duplicated_code.min_lines 10 The fewest lines each copy of a block spans, from its first token to its last: a whole number, 1 or more.
assay:
cross_file:
duplicated_code:
exclude: ["lib/legacy/**"]
min_tokens: 150

See Duplicated code for the full rules and the defaults.

Terminal window
assay cross-file # every cross-file check
assay cross-file duplicated_code # this one alone

The check does not run in the editor and is not part of assay check. Its results never fail the gate; a ceiling on duplicated code is a separate gate condition (see In the gate). At turn end the agent is handed only the duplication the session made (see The turn end).

A suppression comment does not silence a cross-file check. Use the check’s exclude for a copy you keep on purpose, or raise min_tokens.

All cases
  • Each region is one advisory result with the source cross_file, from the first token of its first copy to the last token of the copy that ends last.
  • The message names three other copies by their files and lines, and the rest by their number.
  • Its how-to-fix line is “Move the shared code into one function, widget or class, and use it from each place.”
  • Three copies of a block are three results. The JSON and SARIF results also list the first ten other places, by path and line, in relatedLocations; code scanning shows them with the alert as “another copy”. The console report draws every place of the same code in one numbered group, by its file, its line range and its size, in place of the messages (Duplicated code). The GitHub, GitLab and Markdown reports show only the message.

The check compares the analyzer’s tokens, not text. Comments and layout do not count, and every string literal and every number is one token, so changed labels and sizes still make a copy. Trailing commas, directives and sections of generated code (from a comment that begins with BEGIN GENERATED to one that begins with END GENERATED) are left out. Names stay as written, because Flutter’s widget trees share their shape and trees of different widgets that look alike are not copies.

A block is a run of at least min_tokens tokens (100) at two or more places that do not overlap, each spanning at least min_lines lines (10). Two fixed floors keep out code that is long but not worth merging: the run needs at least 20 distinct tokens and at least 40 distinct shapes. The second floor keeps out code that repeats one short pattern with other names, such as the parameter lists, copyWith, == and toJson of two classes of the same fields.

The copies in one file that share a line make one region. Two copies of the same block are never one region: in one file they are two regions that name each other.

Duplicated code gives the full rules.

The check reads the Dart files under lib/ of each package of the project, nested packages too. It leaves out a file:

  • in assay.exclude or analyzer.exclude (the defaults **/*.g.dart, **/*.freezed.dart, **/*.gen.dart, **/*.mocks.dart and the generated localization files among them);
  • in the check’s own exclude;
  • in a package whose pubspec cannot be read as one that names the package;
  • with a compile error, which is listed as not judged;
  • that looks generated: its comments before the first token say GENERATED CODE, DO NOT EDIT, DO NOT MODIFY, generated by or @generated.

A file left out is not reported, and a copy of its code makes nothing elsewhere reported. Generated code that carries no such header and no BEGIN GENERATED mark, outside the excluded paths, is read like any other: leave it out with exclude.

  • In the project judgement (assay gate) a region is a whole-project result like those of the other cross-file checks: listed as known or new against the baseline, and never failing it.
  • assay baseline update --add records the regions. baseline update and the gate remove the entry of a region that is no longer found.
  • At turn end the agent is handed only the duplication the session made: a region that holds a code line the session wrote, once, with the places of the copies it repeats. A region of a file the session did not touch is never handed over. See The turn end.
  • Every key is a configuration change at the gate: an agent that raises min_tokens or switches the check off is shown it at turn end.

See assay cross-file for the command.

The fingerprint is that of every cross-file result: the rule, the file, the text of the region’s first line and an ordinal. So an edit at the head of a copy, or a copy that grows a line upward, makes a new result and hands both copies over again at turn end. An edit inside a region, or a copy made elsewhere, keeps it.

min_tokens is 100 and min_lines is 10, and the check is on under every preset. See Defaults.

There is no fix, and none is planned: moving copies into one function, widget or class is a design decision, not a mechanical edit.