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(Pro+) (default), json(Teams+), codeclimate(Teams+), gitlab(Teams+), checkstyle(Teams+), sonar(Teams+)]
-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)
-s, --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.
-e, --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.
--exclude-public-api (--ep) Exclude publicly exported code from the analysis output.
--[no-]fatal-unused Treat find unused code as fatal.
(defaults to on)
Suppressing the Command​
To suppress issues produced by the command, add the ignore: unused-code
comment. To suppress the command for an entire file, add the ignore_for_file: unused-code
comment to the beginning of a file.
You can also set the exclude:unused-code:
config entry in the analysis_options.yaml
(this configuration is used by both CLI and the IDE integration).
Excluding Public API​
By default, this command analyzes all code, even if it's part a package's public API.
To exclude such code from the analysis, pass the exclude-public-api
CLI flag.
This command follows this convention when locating API and implementation files.
Overriding "exclude-public-api" for a Particular Package​
To override the exclude-public-api
flag for a particular package, set the exclude-public-api
entry in the analysis_options.yaml
.
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 (default)​
Use --reporter=console
to get output in console format.
JSON​
Use --reporter=json
to get output as a single JSON object containing metadata and the list of unused code issues.
Format specification
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 sourceusedOnlyInTests
a boolean value indicating the code is only used in test files
{
"declarationType": "extension",
"declarationName": "StringX",
"offset": 156,
"line": 7,
"column": 1,
"usedOnlyInTests": false
}
GitLab​
Use --reporter=gitlab
to get output in a GitLab-compatible format. To learn how to integrate DCM with GitLab, refer to this guide.
Code Climate​
Use --reporter=codeclimate
to get output in Code Climate
format.
Output example
{"type":"issue","check_name":"unused-code-issue","description":"Unused field buttonRect","categories":["Bug Risk"],"location":{"path":"lib/src/buttons/pulldown_button.dart","positions":{"begin":{"column":3,"line":41},"end":{"column":25,"line":41}}},"severity":"major","fingerprint":"46ed0a539d46e514e67003f0a661976e"}
{"type":"issue","check_name":"unused-code-issue","description":"Unused field constraints","categories":["Bug Risk"],"location":{"path":"lib/src/buttons/pulldown_button.dart","positions":{"begin":{"column":3,"line":42},"end":{"column":36,"line":42}}},"severity":"major","fingerprint":"a9a5952f5de9e4c94a4c638d2410c7a4"}
Checkstyle​
Use --reporter=checkstyle
to get output in Checkstyle
format.
Output example
<?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>
Checkstyle
format is supported by Bitbucket. To learn how to integrate DCM with Bitbucket, refer to this guide.
Sonar​
Use --reporter=sonar
to get output in SonarQube's generic format for external issues.
Output example
{
"rules": [
{
"cleanCodeAttribute": "CLEAR",
"description": "To learn more, visit the documentation https://dcm.dev/docs/cli/code-quality-checks/unused-code/",
"engineId": "dcm",
"id": "unused-code-issue",
"impacts": [
{
"severity": "MEDIUM",
"softwareQuality": "MAINTAINABILITY"
}
],
"name": "unused-code-issue"
}
],
"issues": [
{
"effortMinutes": 10,
"primaryLocation": {
"filePath": "lib/src/home_i18n.dart",
"message": "Unused getter homeTab",
"textRange": {
"endColumn": 9,
"endLine": 14,
"startColumn": 3,
"startLine": 10
}
},
"ruleId": "unused-code-issue"
},
{
"effortMinutes": 10,
"primaryLocation": {
"filePath": "lib/src/home_i18n.dart",
"message": "Unused getter profileTab",
"textRange": {
"endColumn": 9,
"endLine": 20,
"startColumn": 3,
"startLine": 16
}
},
"ruleId": "unused-code-issue"
}
]
}