Skip to content

Cross-file checks

A cross-file check looks at the whole project, for a result that no single file can decide. assay has five. All are advisory and have no fix. They run in the command-line program only: nothing of them appears in the editor or in dart analyze.

Check Reports
unused_code A declaration nothing in the project refers to: a top-level type, extension, type alias, function or variable, or a member, named constructors and enum values among them. A private one that the analyzer itself reports is left to it (see Left to the SDK).
unused_files A Dart file no entry point reaches through imports, exports, parts and every branch of a platform-dependent import.
unused_l10n A localization entry nothing in the project uses: a member of the class Flutter’s localization generator writes (flutter gen-l10n, ARB files, AppLocalizations), or a key of the JSON translation files of easy_localization.
unnecessary_nullable A parameter declared nullable to which every call in the project passes a value that cannot be null.
duplicated_code A region of the shipped code (lib/ of each package) that copies of a block of code found elsewhere in it cover, with the places of the other copies. See Duplicated code.
Terminal window
assay cross-file # every check
assay cross-file unused_files # one check
assay cross-file --reporter json # the result schema, with the crossFileChecks field
assay cross-file --reporter sarif:build/cross-file.sarif
╭─ assay cross-file ───────────────────────────────────────────────────────────╮
│ Preset recommended │
│ Checks unused_code, unused_files, unused_l10n, unnecessary_nullable, │
│ duplicated_code │
│ Files 5 │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Violations ────────────────────────────────────────────────── 6 in 4 files ─╮
│ Line Rule Tier │
│ ──────────────────────────────────── │
│ lib/format.dart 1 violation │
│ ! 5:8 unused_code advisory │
│ Nothing in the project refers to the function `signed`. │
│ │
│ lib/greeting.dart 2 violations │
│ ! 2:22 unnecessary_nullable advisory │
│ The parameter `name` of the function `greet` is nullable, but every │
│ call in the project passes it a value that cannot be null. │
│ ! 5:24 unnecessary_nullable advisory │
│ The parameter `name` of the function `welcome` is nullable, but │
│ every call in the project passes it a value that cannot be null. │
│ │
│ lib/orphan.dart 1 violation │
│ ! 1:1 unused_files advisory │
│ │
│ lib/prices.dart 2 violations │
│ ! 14:8 unused_code advisory │
│ Nothing in the project refers to the function `name`. │
│ ! 17:8 unused_code advisory │
│ Nothing in the project refers to the function `label`. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Checks ─────────────────────────────────────────────────────────────────────╮
│ Check Tier Count │
│ ───────────────────────────────────── │
│ unused_code advisory 3 │
│ → Delete the declaration if nothing needs it. │
│ │
│ unnecessary_nullable advisory 2 │
│ → Make the parameter non-nullable if no caller needs to pass null. │
│ │
│ unused_files advisory 1 │
│ No entry point reaches this file through imports, exports or parts. │
│ → Delete the file if nothing needs it, or import it where it is used. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ ✓ PASS ─────────────────────────────────────────────────────────────────────╮
│ 0 blocking · 6 advisory · 4 of 5 files │
╰──────────────────────────────────────────────────────────────────────────────╯
The cross-file pass read 5 Dart files and took 0.9 s.

cross-file exits with 0 whatever the checks find, also with files that do not compile and with a check that failed; 2 on a configuration or usage error; 3 when it could not run. It takes no paths: it always reads the whole project. assay check does not run the checks. See assay cross-file.

Each check has enabled and exclude under assay.cross_file, and <check>: false is the short form. All five are on under every preset. duplicated_code also has min_tokens and min_lines (see Configuring checks).

A suppression comment does not silence a cross-file result. Use the check’s exclude, or switch the check off:

assay:
cross_file:
unused_files:
exclude: ["tool/templates/**"]
unnecessary_nullable: false
  • The check’s exclude places no result in the files it covers. For duplicated_code it takes the files out of the detection altogether: a copy there is neither reported nor makes the code it copies reported elsewhere.
  • assay.exclude and analyzer.exclude also keep results out of the files they cover. unused_l10n is the exception: it reports the members of Flutter’s generated localization class at their declaration in that class (app_localizations.dart), although the default assay.exclude covers it, because that is where the entries are. Only the check’s own exclude silences it there.

Cross-file results never fail the gate, at turn end or in the project judgement. A ceiling on duplicated code is a separate gate condition, judged only when asked for (see Duplicated code in the gate).

At turn end the gate runs the five checks on the whole project, beside its two engines, so the agent is told what its change left behind anywhere in the project. Remove the last caller of a function, and the function appears in the message as unused. Copy a widget from one screen into another, and the region of the new copy appears, naming the copy it repeats; the region of the screen it was copied from does not, though it is new to the project as well.

  • Only new results reach the agent. At session start the agent plugin takes the session mark with assay mark --cross-file, which runs the pass once. The turn end hands over only results whose fingerprint is neither in the mark nor in the baseline, each once. So standing dead code stays out of the message, even in a project without a baseline, and so does a result the change removes and brings back as it was. The full report still lists everything.
  • A duplicated region is handed over only where it holds a code line the session wrote, wherever the session moved its first line (see Duplicated code).
  • A session first seen late, as where .dart_tool/assay_agent was removed during the session or the plugin was enabled after it started, has its mark taken then, by the same pass. What stands at that moment, the agent’s work up to then included, is not handed over.
  • In the message they are the last section, before the foot line. Compile errors in files the change did not touch, which the pass sees, are reported there too and do not fail the gate; a compile error in a changed file still fails it. On a passing gate with nothing new, the agent stops.
  • The project judgement (assay gate) includes the cross-file results, marked known or new, and counts them in summary.crossFile.
  • The baseline. assay baseline update --add records cross-file results. The gate and baseline update remove the entries that no longer match after a run of the pass; check does not run the pass, and leaves them.

A check switched off does not run in the gate. A turn end whose change holds a Dart, YAML, JSON or ARB file runs a whole-project pass. A change with none of those files runs no pass, nor does a gate under which every cross-file check is switched off.

The five checks share one pass over every Dart file of the project, generated files and files in excluded paths among them, so that a use from generated code counts as a use. A use from a test counts too. A file that does not compile is listed as not judged; the pass still uses what the analyzer could read of it, so that nothing elsewhere is reported because of it.

  • duplicated_code reads only the files under lib/ of each package that compile and do not look generated.
  • unused_l10n places the results of easy_localization at the key’s line in a JSON translation file: the fallback locale’s, else the start locale’s, else the first that holds the key.

What counts as used from outside depends on the package’s pubspec:

  • Marked publish_to: none (an application, an internal package): nothing outside the project can import it. Every unused declaration is reported, public or not, and so is a file in lib/ that no entry point reaches, outside lib/src/ as well.
  • Publishable: any other publish_to, or no publish_to line at all, as pub treats it. Every library in lib/ outside lib/src/ is public API: it is an entry point for unused_files, and what it exports is left out of unused_code and unnecessary_nullable.

One member of a pub workspace judged on its own also counts as publishable, because the other members use it from outside. Judge the workspace from its root to have those uses seen.

The analyzer reports some unused declarations itself, with the warnings unused_element and unused_field (for a field or an enum value), both on by default: a declaration that cannot be reached from outside its library and that nothing in the library uses. One defect at one place is one violation, so unused_code leaves those to the SDK:

  • Left to the SDK: private top-level declarations; private members, static members of a private type, and the members of a private or unnamed extension; a constructor with a name that is private, belongs to a private type, or is a generative constructor of an enum, where its type has another constructor too.
  • Kept by unused_code: public top-level declarations, and public instance members of any type; extensions and class aliases (class A = B with M;); the only constructor of a type; and a declaration the analyzer counts as used and unused_code does not, such as one that refers to itself from a function declared inside it.

This holds where the SDK that runs assay reports the warning there (every SDK from 3.11 on does), and analyzer.errors does not set it to ignore. Then neither cross-file nor the gate reports the declaration. The standard analysis reports it at its name, as a warning, which blocks in the gate on a changed file.

To have unused_code report these declarations itself, ignore the warning:

analyzer:
errors:
unused_element: ignore # unused_code reports these declarations itself

Another severity (info) leaves the places to the warning. Ignoring only unused_field gives the fields and the enum values back to unused_code.

More of the split under Left to the SDK on the page of unused_code:

  • On Dart 3.11 the analyzer also reports a constructor that a mixin application forwards (from 3.12 on it counts it as used), and unused_code keeps it on every release: two violations there on 3.11.
  • unused_code reads the project’s root options file for the warning’s severity, not a nested analysis_options.yaml.