Check Unused Code
Checks unused classes, fields, properties, methods, functions, variables, extensions, enums, mixins and type aliases.
To execute the command, run:
$ dcm check-unused-code lib # or dcm uc lib
Full command description:
Usage: dcm check-unused-code [arguments] <directories>
-h, --help Print this usage information.
--no-exclude-overridden Do not treat overridden members as always used.
-r, --reporter=<console> Analysis output format.
[console (default), json, codeclimate, gitlab, checkstyle]
-a, --absolute-path Show absolute paths in console reporter output.
--json-path=<path/to/file.json> Path to the JSON file with the analysis output.
-c, --print-config Print resolved config.
--root-folder=<./> Root folder.
(defaults to the current directory)
--sdk-path=<directory-path> Dart SDK directory path.
If the project has a `.fvm/flutter_sdk` symlink, it will be used if the SDK is not found.
--exclude=<{**/*.g.dart,**/*.freezed.dart}> File paths in Glob syntax to be exclude.
(defaults to "{**/*.g.dart,**/*.freezed.dart}")
--no-congratulate Don't show output even when there are no issues.
--verbose Show verbose logs.
--ci-key The license key to run on CI server. Can be provided via DCM_CI_KEY env variable.
--email The email used to purchase the license. Can be provided via DCM_EMAIL env variable.
--no-analytics Disable sending anonymous usage statistics.
--monorepo Include publicly exported code into the check.
--[no-]fatal-unused Treat find unused code as fatal.
(defaults to on)
Suppressing the command
In order to suppress the command add the ignore: unused-code
comment. To suppress for an entire file add ignore_for_file: unused-code
to the beginning of a file.
You can also set unused-code-exclude:
config entry in the analysis_options.yaml
(this configuration is used by both CLI and the IDE integration).
Monorepo support
By default, this command treats all code that is exported from the package as used. It uses check-exports-completeness
results and won't report even transitive public entities that are not exported directly.
This command also follows this convention of locating implementation files in the lib/src/
folder. If you don't follow this convention, you will need to pass the --monorepo
flag for this command to work.
To disable this behavior use --monorepo
flag. This might be useful when all the packages in your repository are only used within the repository and are not published to the pub.
Including class member hierarchies into the analysis
By default, this command does not analyze overridden members as they can be used implicitly through the parent class.
To include overridden members into the analysis pass the --no-exclude-overridden
flag.
This mode is experimental and may have false positives. If you encounter any issues, please post them on our Discord.
This mode won't analyze external code (from pub
or flutter
) or code that extends external code.
Output example
Console
Use --reporter=console
to enable this format.
JSON
The reporter prints a single JSON object containing meta information and the unused code file paths. Use --reporter=json
to enable this format.
The root object fields are
formatVersion
- an integer representing the format version (will be incremented each time the serialization format changes)timestamp
- a creation time of the report in YYYY-MM-DD HH:MM:SS formatunusedCode
- an array of unused code issues
{
"formatVersion": 2,
"timestamp": "2021-04-11 14:44:42",
"unusedCode": [
{
...
},
{
...
},
{
...
}
]
}
The unusedCode object fields are
path
- a relative path of the unused fileissues
- an array of issues detected in the target file
{
"path": "lib/src/some/file.dart",
"issues": [
...
],
}
The issue object fields are
declarationType
- unused declaration typedeclarationName
- unused declaration nameoffset
- a zero-based offset of the class member location in the sourceline
- a zero-based line of the class member location in the sourcecolumn
- a zero-based column of class member the location in the source
{
"declarationType": "extension",
"declarationName": "StringX",
"offset": 156,
"line": 7,
"column": 1
}
GitLab
Reports unused code issues in merge requests based on Code Quality custom tool. Use --reporter=gitlab
to enable this format.
Checkstyle
Use --reporter=checkstyle
to enable this format.
<?xml version="1.0"?>
<checkstyle version="10.0">
<file name="example.dart">
<error line="5" column="3" severity="warning" message="Unused class SomeClass" source="unused-code-issue"/>
</file>
</checkstyle>