Skip to main content
Teams

Check Parameters

Checks parameters in functions, methods and constructors for various issues.

By default shows optional parameters that are never passed, parameters with unnecessarily nullable types, and optional parameters that are always passed and therefore unnecessarily marked as optional.

Additional checks are available under the corresponding options.

To execute the command, run:

$ dcm check-parameters lib # or dcm p lib

Full command description:

Usage: dcm check-parameters [arguments] <directories>
-h, --help Print this usage information.


--show-same-value Include parameters that always get the same constant argument.
--show-unused-default-value Include parameters with default values that always get an argument.
--show-redundant Include parameters whose values are always passed with another parameter.
--show-unused-vft Include @visibleForTesting parameters that are never used in tests.
--show-broad-types Include parameters whose types can be made more specific.


-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}> Files to exclude (in Glob syntax).
(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-found Treat parameter issues as fatal.
(defaults to on)

Additional modes

All additional modes are currently experimental and can change in the future releases.

Parameters with the same value

Shows parameters that always receive the same constant value.

For example, passing the same constant string to all invocations of a function allows the parameter to be removed and its usage to be replaced by the passed constant value.

To highlight such parameters, pass the --show-same-value flag.

Parameters with unused default values

Shows parameters with default values that always receive an argument making the default value redundant.

To highlight such parameters, pass the --show-unused-default-value flag.

Redundant parameters

Shows parameters that are a field of another passed parameter and can be removed.

For example, if you pass a list as an argument and then pass its length as another argument to all invocations of a function, the parameter for length is redundant.

To highlight such parameters, pass the --show-redundant flag.

Parameters with unused "@visibleForTesting"

Shows all parameters that are not used in tests, but are annotated with @visibleForTesting.

To highlight such parameters, pass the --show-unused-vft flag.

Parameters with broad types

Shows parameters that have a broad type (based on passed arguments).

For example, if a function declaration has a parameter with the Object type, but all arguments passed for that parameter have the String type, such parameter will be considered as a broad-type parameter.

To highlight such parameters, pass the --show-broad-types flag.

Suppressing the command

In order to suppress the command globally, add the ignore_for_file: parameters comment.

For local ignores, each separate issue type supports its own ignore:

  • unnecessary-nullable-parameters
  • never-passed-parameters
  • unnecessary-optional-parameters
  • same-value-parameters
  • redundant-default-value-parameters
  • redundant-parameters
  • unused-visible-for-testing-parameters
  • broad-type-parameters

Monorepo support

warning

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.

Overriding monorepo mode for a particular package

To override the monorepo mode for a particular package, set the monorepo entry in the analysis_options.yaml.

For example, if publicly exported files of a particular package are not expected to be changed, setting the monorepo entry to false and passing the --monorepo option will enable the monorepo mode for all other packages, but not the one with monorepo: false.

Output example

Console

Use --reporter=console to enable this format.

Console

JSON

The reporter prints a single JSON object containing meta information and the parameter issues. 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 format
  • parameters - an array of parameter issues
{
"formatVersion": 2,
"timestamp": "2021-04-11 14:44:42",
"parameters": [
{
...
},
{
...
},
{
...
}
]
}

The parameters object fields are

  • path - a relative path of the file with the parameters declaration
  • issues - an array of issues detected in the target class
{
"path": "lib/src/some/class.dart",
"issues": [
...
],
}

The issue object fields are

  • declarationName - the name of a declaration with parameter issues
  • declarationType - the type of a declaration with parameter issues (function, method or constructor)
  • parameterName - the name of the parameter
  • parameterType - the type of the parameter
  • type - the type of the issue ("never-passed", "unnecessary-nullable", "same-value", "redundant-default-value", "unnecessary-optional", "unused-visible-for-testing", "broad-type", "redundant")
  • offset - a zero-based offset of the class member location in the source
  • line - a zero-based line of the class member location in the source
  • column - a zero-based column of class member the location in the source
{
"declarationName": "someFunction",
"declarationType": "function",
"parameterName": "value",
"parameterType": "String?",
"type": "unnecessary-nullable",
"offset": 156,
"line": 7,
"column": 1
}

GitLab

Reports parameter 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="../abstract_class.dart">
<error line="0" severity="warning" message="This parameter always gets the same value" source="redundant-default-value"/>
</file>
<file name="../class_with_factory_constructors.dart">
<error line="0" severity="warning" message="This parameter is unnecessary nullable" source="unnecessary-nullable-parameter-issue"/>
</file>
</checkstyle>