unused_code
Reports a declaration that nothing in the project refers to. Delete it, or exclude it if you keep it on purpose.
- Kind
- cross-file check
- Tier
- advisory
- Host
- command-line program only (
assay cross-file) - Fix
- none
What it reports
Section titled “What it reports”A class, mixin, enum, extension, type alias, top-level function or variable, getter, setter, method, field, constructor with a name or enum value that nothing in the project refers to. The message names the kind and the declaration:
Nothing in the project refers to the method
Cart.unusedMember.
- Any reference counts as a use, from anywhere in the project: tests, examples, scripts and generated code too.
- A declaration that refers only to itself is unused, such as a recursive function nothing else calls.
- In a package you publish, its public API is left out: what its public libraries (in
lib/, outsidelib/src/) export, and the public members of public types. A package with nopublish_toline counts as published. Mark an app or an internal packagepublish_to: noneto have its public declarations judged.
It does not report:
- entry points such as
main, and declarations that@pragma('vm:entry-point')keeps; - a declaration with an annotation of code generation or of a framework (
@JsonSerializable,@freezed,@riverpod,@RoutePage, …), with every member of it; - overrides and the members they override:
build,initState,dispose,toString,==, …; - a private declaration that the analyzer’s own warning
unused_elementorunused_fieldreports (see Left to the SDK); - anything in a file that
unused_filesreports, or in a file that does not compile.
Dead code is read, searched and kept compiling through every refactoring, and it misleads: whoever finds a helper assumes it is used, and an agent copies the pattern of a method that no longer runs. Code written by an agent leaves such declarations behind, like the first version of a helper next to the one that replaced it, and no single file shows it. The check is advisory, since some declarations are kept on purpose, such as a debugging helper; each project decides.
Configuration
Section titled “Configuration”Under the assay: key of the project’s root analysis_options.yaml. The check is on under every preset unless switched off.
| Key | Default | Effect |
|---|---|---|
assay.cross_file.unused_code.enabled |
true |
false switches the check off. The short form unused_code: false means the same. |
assay.cross_file.unused_code.exclude |
[] |
Path globs, read from the project root, where the check places no result. The files are still read, and what they use counts. |
assay: cross_file: unused_code: exclude: - lib/debug/**Running it
Section titled “Running it”assay cross-file runs every cross-file check on the whole project, and assay cross-file unused_code runs this one alone. The check does not run in the editor and is not part of assay check. The gate runs it with the other cross-file checks: its results never fail the gate, and they pass through the baseline (see In the gate).
Suppressing it
Section titled “Suppressing it”A suppression comment does not silence a cross-file check. Use the check’s exclude for a file that holds declarations kept on purpose, or switch the check off.
Details
Section titled “Details”All cases
The result
Section titled “The result”The result is a violation with the source cross_file, the rule unused_code and the advisory tier. It stands at the declaration’s name, or at the extension keyword of an extension without a name. Its how-to-fix line reads “Delete the declaration if nothing needs it.”
Its fingerprint is made of the declaration’s line, as a rule’s is, so it stays the same while other lines of the file change.
Which declarations it covers
Section titled “Which declarations it covers”| Kind | Named in the message as |
|---|---|
| class, class alias | class |
| mixin | mixin |
| enum | enum |
| extension, with a name or without one | extension (one without a name as extension on <type>) |
| extension type | extension type |
type alias (typedef) |
type alias |
| top-level function | function |
| top-level variable | variable |
| getter, top-level or a member | getter |
| setter, top-level or a member | setter |
| method, static or not | method (as Type.method) |
| field, static or not | field (as Type.field) |
| constructor with a name | constructor (as Type.name) |
| enum value | enum value (as Enum.value) |
Not covered:
- an operator, which is used through an expression that does not name it;
- a constructor without a name, which a subclass’s constructor calls where no code names it;
- the representation field of an extension type;
- local variables and functions, parameters and type parameters.
A field, its getter and its setter are one declaration, and so are an explicit getter and setter of one name. A use of either is a use of both, and only the first is reported.
What counts as a use
Section titled “What counts as a use”- Any reference from outside the declaration itself: a call, a read, a write, a tear-off, a type annotation, an
isorastest, a pattern, a constructor call,super.member, an initializing formalthis.field, a field initializer, a reference in a documentation comment ([Foo]). - A declaration that refers only to itself is unused. A recursive function nothing else calls is reported, and so is a class that only its own members name. Two declarations that only refer to each other count as used; once one of them is deleted, the next run reports the other.
- A use of a member is a use of its type or extension. An extension is used when a member of it is, also through the implicit form
value.extensionMember(). - A use from anywhere in the project counts: a test, a benchmark, an example, a script.
- A use from generated code counts. The pass reads every Dart file of the project, generated files and files in
assay.excludeandanalyzer.excludeamong them. So a declaration that only its generatedpartrefers to (the_$FooFromJsona.g.dartpart calls back, a private field generated code reads) is used. - A use from a file that
unused_filesreports still counts. The declarations it uses appear once you delete the file and run the check again.
What is never reported
Section titled “What is never reported”A declaration is not reported in these cases:
- Entry points: a top-level
main, wherever it is;testExecutableof aflutter_test_config.dart; the top-levelpluginoflib/main.dart, by which the analysis server loads an analyzer plugin. - A declaration that
@pragma('vm:entry-point')keeps, which the Dart VM calls from outside Dart, and every member of a type it is written on. - A declaration with an annotation of code generation or of a framework: any annotation that does not come from the Dart SDK,
package:metaor Flutter. That covers@JsonSerializable,@JsonKey,@freezed,@riverpod,@injectableand@singleton,@RoutePage,@HiveType, a widgetbook use case and the like, and an annotation the project declares for its own generator. Such a declaration is kept with every member of it, also where the generated code is not on disk. Of Flutter’s own annotations, a widget preview (@Preview) keeps alive too, since the Flutter tool finds it. An annotation that does not resolve keeps alive as well. - Generated code, in short: a declaration that generated code on disk refers to is used, like any other. Where the generated part is not on disk, its library does not compile and is not judged, so nothing in it is reported. A declaration elsewhere that only the missing code would call is kept by its annotation.
- Overrides and what they override: a member that overrides or implements a supertype member, a member a subtype overrides or implements, and a member a class inherits in place of an interface member (
class B extends A implements I, whereA.mstands in forI.m). Any of them can be called through another. This covers every override ofObjectand framework members:build,initState,dispose,createState,toString,==,hashCode. - Members called by name from outside the project: a member named like a member of
Object(toString,hashCode,noSuchMethod,runtimeType),call(a function value calls it),toJson(jsonEncodecalls it) andfromJson. - A name the project uses without the analyzer knowing what it names: a member called on a
dynamicvalue, a name that does not resolve in a file that does not compile, and the names of a symbol literal (#name). Every declaration of that name is kept, whatever its type. - The values of an enum used as a whole: through its
values(and sovalues.byName), or by aswitchover a value of the enum’s type. A value of an enum used only by name is reported when nothing names it. - A constructor named
_without parameters: it keeps a class from being made or extended outside its library, and nothing is meant to call it. - The public API of a package that has one (see The two package types).
- Nothing in a file that only a condition of an import or an export names. In
import 'io.dart' if (dart.library.js_interop) 'web.dart';the analyzer resolves the project againstio.dart, so every use goes there, andweb.dartstands in for it on another platform. A file that both branches import is judged by the branch the analyzer resolves. - Nothing in a file that
unused_filesreports: the file is reported as a whole, not declaration by declaration. This holds also whereunused_filesis switched off or excludes the file. - No member of a type or an extension that is reported itself.
- An operator, and a constructor without a name.
- Nothing in a file that does not compile, which is listed as not judged, and nothing in an excluded path (see Where no result is placed).
- A declaration that the analyzer’s own warning reports,
unused_elementorunused_field(see the next section).
A declaration that only code outside Dart reaches by name is reported unless an annotation keeps it: a class named by dartPluginClass in a plugin’s pubspec, a function native code looks up by name, a member found through dart:mirrors, a declaration a builder named in build.yaml reads. Keep it with @pragma('vm:entry-point'), or with the check’s exclude.
Left to the SDK
Section titled “Left to the SDK”The analyzer reports, by itself, an unused declaration that cannot be reached from outside its library: the warning unused_element, and unused_field for a field or an enum value. Both are on by default and stand at the declaration’s name, where this check would place its result. One defect at one place is one violation, so the check leaves those to the SDK, in cross-file and in the gate alike. The standard analysis reports the warning there, which blocks in the gate on a changed file.
This holds where both are true:
- the SDK that runs assay reports the warning: every release from 3.11 on does, in the same form on 3.11 and 3.14;
analyzer.errorsdoes not set the warning toignore.
Under a configuration in error, no cross-file check runs at all.
| Left to the SDK | Warning |
|---|---|
a private top-level class, mixin, enum, extension type, type alias (typedef, either form), function, variable, getter or setter, in the defining file or in a part |
unused_element |
| a private method, getter or setter, static or not; a static method, getter or setter of a private class, mixin, enum or extension type; every method, getter and setter of a private extension or one without a name | unused_element |
| a private field, static or not; a static field of a private type or extension; a private enum value; every value of a private enum | unused_field |
| a constructor with a name that is private, belongs to a private type, or is a generative constructor of an enum, public or not, where its type has another constructor too | unused_element |
The check keeps reporting, on every SDK:
- every public top-level declaration, and a public instance member of any type, private or not, which a value of the type or of a public subtype can reach from outside; a public member of a public extension;
- an extension, named or not, with members or not. The analyzer reports none, and instead reports each unused member of a private or unnamed extension at the member’s name.
- a class alias (
class A = B with M;), which the analyzer does not judge, and a class marked@JS, which it counts as used; - the only constructor of a type (
Lone._only(int value)), which the analyzer takes for one that keeps the type from being made; - a declaration that the analyzer counts as used and this check does not. The analyzer ignores a reference to a class from inside the class, and to a function or method from its own body. But it counts:
- a reference from a function declared inside it:
int _f() { int again() => _f(); ... }; - a reference to a mixin, an enum or an extension type from inside itself:
static _Mode parse(...) => _Mode.values...; - a variable read in its own initializer, and a getter read by its setter (never one in a comment);
- every public constructor of a class that a public type alias names (
typedef Public = _Hidden;); - from 3.12 on, a constructor that a mixin application forwards, where the constructor that forwards to it is called. On 3.11 the analyzer reports such a constructor too, so it has two violations there.
- a reference from a function declared inside it:
To have the check report these declarations itself, ignore the warning under analyzer.errors. Ignoring unused_element gives back every form of it; ignoring unused_field gives back the fields and enum values. Another severity (info, error) leaves the places to the warning. The check reads analyzer.errors from the project’s root options file only, so a nested analysis_options.yaml that ignores the warning leaves those places unreported.
A baseline entry of the check at a place now left to the SDK no longer matches, and the gate and baseline update remove it after a run of the pass.
The two package types
Section titled “The two package types”Which declarations are public API depends on the pubspec of the file’s package: the nearest directory above it, inside the project, that holds a pubspec.yaml. See also The two package types.
| Package | What is left out as public API |
|---|---|
marked publish_to: none (an application, an internal package) |
nothing: nothing outside the project can use the package, so every unused public declaration is reported |
publishable: any other publish_to, or no publish_to line at all |
every declaration that a public library (a Dart file in lib/ outside lib/src/) exports, its own public declarations among them; every declaration a library in lib/src/ exports from other libraries, since another package can import that library all the same; and every public member of a public type, wherever the type is declared, since an instance of it can reach a user through the API |
the project root’s package, when its pubspec says resolution: workspace (one member of a pub workspace judged on its own) |
the same as a publishable package: the other members use its API from outside the project. Judge the workspace from its root to have them seen. |
| a directory with no pubspec | nothing |
A package with no publish_to line counts as publishable, as pub treats it. So in an unmarked internal package, an unused public class that a public library exports is not reported. Mark such a package publish_to: none to have it judged as an application.
In a publishable package the check still reports a private declaration nothing uses, a public top-level declaration of lib/src/ that no library exports, an unused public type that is not exported (with none of its members), and every unused declaration in test/, bin/, tool/ and the other directories outside lib/. Where other packages import such a library of lib/src/ directly, put it in the check’s exclude.
A pubspec that cannot be read
Section titled “A pubspec that cannot be read”A pubspec that cannot be read as a YAML map that names the package leaves its package type unknown: one that is not valid YAML (as an edit in progress may leave it), is empty or unreadable, or has no name. Then no cross-file check judges a Dart file of that package in that run:
- nothing is reported there, and the files count as not checked;
- the gate and
baseline updateremove none of their entries, so nothing comes back as new once the pubspec is whole again; - the run says why in one line: the
Not checkedblock ofcross-file, or aCross-file checks:note in the gate’s full report.
The packages beside it are judged as usual. A result in another package that only the public API of such a package keeps (a file it reaches, a declaration it re-exports) can still be missed in that run.
Where no result is placed
Section titled “Where no result is placed”- In a file that
assay.excludeoranalyzer.excludecovers (with the defaultassay.exclude, no generated file). Such a file is still read, and what it uses counts. - In a file that the check’s own
excludecovers. - In a directory that holds no source of the project:
.dart_tool, a directory directly under the project root whose name starts with a dot, and the otherscheckleaves out. - In a file that is not judged.
No fix
Section titled “No fix”No fix exists, and none is planned: code is never deleted mechanically. A declaration that looks unused can be reached by something the check does not see, and deleting one can leave others unused that the next run reports.