avoid_unused_parameters
Reports a parameter that the body of its declaration never uses. Remove it, use it, or name it _ where the signature is fixed.
- Tier
- advisory
- Scope
- Dart
- Fix
- none
What it reports
Section titled “What it reports”A parameter of a function, method, setter, operator or constructor whose name stands nowhere in the body. For a constructor the body includes the initializers. Positional, optional and named parameters are judged alike.
// unused is reportedint twice(int value, int unused) => value * 2;
class Point { // scale is reported Point(int x, int y, int scale) : x = x, y = y;
final int x; final int y;}It does not report:
- a parameter of an override, with or without
@override; - a wildcard name,
_,__, …; - a declaration with no body: an abstract member, an
externaldeclaration, a redirecting factory; this.xandsuper.x;- a parameter of a function literal,
items.map((item) => 1); - a parameter of a function used as a value in the same file, such as
stream.listen(onEvent); - a parameter of a function type,
void Function(int code), and the representation of an extension type.
A parameter that nothing reads is a promise the declaration does not keep: the caller computes a value and expects it to matter. It is often the trace of an unfinished change, a body simplified while its signature was not. The rule is advisory because a signature is sometimes fixed from outside in a way the rule cannot see (see What it reports in full).
The SDK lint avoid_unused_constructor_parameters reports the constructor case alone. This rule covers it, so assay’s presets leave that lint out: with both on, the same place is reported twice.
Example
Section titled “Example”double priceWithTax(double price, double rate, String currency) { return price * (1 + rate);}double priceWithTax(double price, double rate) { return price * (1 + rate);}Where the signature is fixed from outside, a positional parameter is named _:
void onTick(Duration _) { refresh();}Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
assay: rules: avoid_unused_parameters: exclude: ["lib/generated/**"]Suppressing it
Section titled “Suppressing it”The violation is at the name of the parameter, so the suppression goes above that line, which in a long parameter list is not the line the declaration starts on. A named parameter cannot be a wildcard, so suppress it where its signature is fixed. Write why on the line directly above the suppression:
// The router calls every page builder with these two named arguments.// ignore: assay/avoid_unused_parameters// ignore_for_file: assay/avoid_unused_parameters silences it in a whole file, and // ignore: avoid_unused_parameters without the prefix works too. See Suppressions.
Details
Section titled “Details”All cases
What it reports in full
Section titled “What it reports in full”A parameter that its declaration’s body never uses. The declaration is a function, top-level or local, a method, static or not, a setter, an operator or a constructor.
- The body is the block, or the expression after
=>. For a constructor it includes the initializers: the field initializers, the asserts, and the call ofsuper(…)orthis(…). - A parameter is used when its name stands anywhere in that body: it is read, assigned, passed on, put into a string, or named inside a nested function. A mention in a documentation comment is no use.
- A use as a value counts in the same file only. A function handed on only from another file, also from another part of the same library, is reported, and so is a public method that another library tears off. Name such a positional parameter
_; a named parameter cannot be a wildcard, so suppress it instead. - A member written for subclasses to override, whose body ignores its parameter, is reported like any other:
void onEvent(Event event) {}. Only the overriding side is exempt. Declare such a member abstract where you can. mainis judged like any function:void main(List<String> arguments)whose body never reads them is reported. Writevoid main().
What it does not report in full
Section titled “What it does not report in full”- A parameter of an override, with or without
@override. A method, a setter or an operator that overrides a member of a superclass, a mixin or an interface must keep that member’s signature. A member counts as an override when its type inherits a member of the same name, whatever the two signatures are; a static method overrides nothing. - A wildcard name, made of underscores only:
_,__,___. It says that the parameter is unused on purpose. - A declaration with no body: an abstract member, an
externaldeclaration, or a redirecting factory,factory Shape(double size) = Square;, which hands its parameters to the constructor it names. this.xandsuper.x. They are used by being declared: one sets a field, the other is passed to the superclass.- A parameter of a function literal,
items.map((item) => 1): the place it is passed to gives its signature. - A parameter of a function, a method or a constructor used as a value in the same file:
stream.listen(onEvent),final handler = onEvent;,[onEvent],return onEvent;,handler == onEvent, a default value{this.handler = onEvent}, a constructor named without arguments,indexes.map(Label.new). Whoever takes the function expects the signature it has. A use as a value is a name that resolves to the declaration and does not stand directly before the argument list of a call of it. It is found by what the name resolves to, not by its spelling: a function that is only ever called is still reported when another function of the same name is handed on. - A parameter of a function type, in a type alias or a function-typed parameter:
void Function(int code). There is no body. - The representation of an extension type,
extension type Id(int value), which declares a field.
Why it matters
Section titled “Why it matters”The caller computes a value, passes it, and expects it to matter, and the reader of the signature assumes it does. Both are wrong, and nothing tells them. The body was simplified and the signature was not; a flag was added for a case that was never written; a value was threaded through three functions to a place that no longer needs it.
Using the parameter
Section titled “Using the parameter”The other way to correct the example, where the parameter was meant to be used:
String priceWithTax(double price, double rate, String currency) { return '${price * (1 + rate)} $currency';}