avoid-unused-constructor-parameters

Same as avoid_unused_constructor_parameters in Dart lints

Group
Style
Path
lint/style/avoid-unused-constructor-parameters
Default
Off, opt in
Origin
Dart lints
Rule
183 of 512

Flags constructor parameters that are never used.

A constructor parameter that is neither read by an initializer nor by the body is dead weight that every caller still has to supply. Initializing formals (this.x) and super parameters (super.x) are always used, and parameters annotated @deprecated/@Deprecated(...) or named only with underscores are skipped, as are redirecting factory and external constructors. Primary header parameters are also read by eager instance field initializers; declaring primary parameters implicitly initialize fields. A constructor with neither initializers nor a body reports every other parameter.

class C {
  final int x;
  C(this.x, String name); // flagged: `name`
}

Invalid

example.dartdart
// @dart = 3.13

class BadOne {
  BadOne(int unused, [String? empty]);
}

class BadTwo {
  int c;

  BadTwo(
    int a,
    this.c,
  );
}

class BadThree {
  final int value;

  BadThree(int input, {int? spare}) : value = input;
}

class BadFour {
  int total = 0;

  BadFour(int first, int second) {
    total = first;
  }
}

class Base {
  Base(this.x);
  final int x;
}

class BadFive extends Base {
  BadFive(super.x, int extra);
}

class BadSix {
  final int value;

  BadSix._(this.value);

  factory BadSix.make(int value, String label) {
    return BadSix._(value);
  }
}

enum BadEnum {
  a(1);

  const BadEnum(int unused);
}

class BadClosureShadow {
  BadClosureShadow(int x) {
    void inner(int x) {
      print(x);
    }

    inner(1);
  }
}

// Primary cases follow the SDK 3.13.5 rule's parameter/initializer scopes.
class PrimaryUnused(int unused);

class PrimaryOptionalUnused([int unused = 0]);

class PrimaryNamedUnused.named({int unused = 0});

class PrimaryPrivateUnused._(int unused) {
  this;
}

class PrimaryPartiallyUsed(int used, int unused) {
  final int value = used;
}

class PrimaryBodyShadow(int value) {
  this {
    void inner(int value) {
      print(value);
    }
    inner(1);
  }
}

class PrimaryFieldShadow(int value) {
  final int Function(int) callback = (int value) => value;
}

class PrimaryMethodScope(int value) {
  int method(int value) => value;
}

class PrimarySiblingScope(int value) {
  PrimarySiblingScope.other(int value) : this(value);
}

int outerValue = 1;

class PrimaryLateScope(int outerValue) {
  late final int value = outerValue;
}

class PrimaryStaticScope(int outerValue) {
  static final int value = outerValue;
}

// INVALID-CODE: non-trivial mixin constructor; analyzer still reports the unused parameter.
mixin class PrimaryMixinUnused(int unused) {}

enum PrimaryEnumUnused(int unused) {
  a(1)
}
The parameter `unused` is not used in the constructor.
3class BadOne {
4 BadOne(int unused, [String? empty]);
The parameter `empty` is not used in the constructor.
3class BadOne {
4 BadOne(int unused, [String? empty]);
The parameter `a` is not used in the constructor.
10 BadTwo(
11 int a,
The parameter `spare` is not used in the constructor.
18
19 BadThree(int input, {int? spare}) : value = input;
The parameter `second` is not used in the constructor.
24
25 BadFour(int first, int second) {
The parameter `extra` is not used in the constructor.
35class BadFive extends Base {
36 BadFive(super.x, int extra);
The parameter `label` is not used in the constructor.
43
44 factory BadSix.make(int value, String label) {
The parameter `unused` is not used in the constructor.
51
52 const BadEnum(int unused);
The parameter `x` is not used in the constructor.
55class BadClosureShadow {
56 BadClosureShadow(int x) {
The parameter `unused` is not used in the constructor.
65// Primary cases follow the SDK 3.13.5 rule's parameter/initializer scopes.
66class PrimaryUnused(int unused);
The parameter `unused` is not used in the constructor.
67
68class PrimaryOptionalUnused([int unused = 0]);
The parameter `unused` is not used in the constructor.
69
70class PrimaryNamedUnused.named({int unused = 0});
The parameter `unused` is not used in the constructor.
71
72class PrimaryPrivateUnused._(int unused) {
The parameter `unused` is not used in the constructor.
75
76class PrimaryPartiallyUsed(int used, int unused) {
The parameter `value` is not used in the constructor.
79
80class PrimaryBodyShadow(int value) {
The parameter `value` is not used in the constructor.
88
89class PrimaryFieldShadow(int value) {
The parameter `value` is not used in the constructor.
92
93class PrimaryMethodScope(int value) {
The parameter `value` is not used in the constructor.
96
97class PrimarySiblingScope(int value) {
The parameter `outerValue` is not used in the constructor.
102
103class PrimaryLateScope(int outerValue) {
The parameter `outerValue` is not used in the constructor.
106
107class PrimaryStaticScope(int outerValue) {
The parameter `unused` is not used in the constructor.
111// INVALID-CODE: non-trivial mixin constructor; analyzer still reports the unused parameter.
112mixin class PrimaryMixinUnused(int unused) {}
The parameter `unused` is not used in the constructor.
113
114enum PrimaryEnumUnused(int unused) {

Valid

example.dartdart
// @dart = 3.13

class Good {
  final int a;

  Good(this.a);
}

class UsedInInitializer {
  final int value;

  UsedInInitializer(int input) : value = input * 2;
}

class UsedInBody {
  int total = 0;

  UsedInBody(int first) {
    total = first;
  }
}

class UsedInAssert {
  UsedInAssert(int n) : assert(n > 0);
}

class Base {
  Base(this.x);
  final int x;
}

class UsedInSuper extends Base {
  UsedInSuper(int x) : super(x + 1);
}

class SuperParameter extends Base {
  SuperParameter(super.x);
}

class Deprecations {
  Deprecations(@deprecated int old, @Deprecated('use other') int older);
}

class Underscores {
  Underscores(int _, int __);
}

class Redirecting {
  Redirecting.make();
  factory Redirecting(int ignored) = Redirecting.make;
}

class External {
  external External(int value);
}

class UsedInClosure {
  void Function() callback = () {};

  UsedInClosure(int value) {
    callback = () => print(value);
  }
}

class Redirect {
  final int value;

  Redirect(this.value);
  Redirect.twice(int value) : this(value * 2);
}

class PrimaryDeclaring(var int value);

class PrimaryFinalDeclaring([final int value = 0]);

class PrimaryInitializing(this.value) {
  final int value;
}

class PrimarySuper(super.x) extends Base;

class PrimaryInitializer(int value) {
  this : stored = value;
  final int stored;
}

class PrimaryAssert({int value = 1}) {
  this : assert(value > 0);
}

class PrimaryBody({int value = 0}) {
  this {
    print(value);
  }
}

class PrimaryField(int value) {
  final int stored = value;
}

class PrimaryFieldClosure(int value) {
  final int Function() callback = () => value;
}

class PrimaryBodyClosure(int value) {
  this {
    void inner() {
      print(value);
    }
    inner();
  }
}

// A body-local shadow must not hide a parameter in a later field initializer.
class PrimarySeparateScopes(int value) {
  this {
    {
      int value = 1;
      print(value);
    }
  }
  final int stored = value;
}

class PrimaryNamedUsed.named(int value) {
  final int stored = value;
}

class PrimaryPrivateUsed._(int value) {
  this : assert(value > 0);
}

class PrimaryDeprecated(@deprecated int old, @Deprecated('use other') int older);

class PrimaryUnderscores(int _, int __);

extension type PrimaryRepresentation(int value) {}

extension type PrimaryPrivateRepresentation._(int value) {}

mixin class PrimaryMixinTrivial();

enum PrimaryEnumField(int value) {
  a(1);
  final int stored = value;
}

Configure avoid-unused-constructor-parameters

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-unused-constructor-parameters": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-unused-constructor-parameters: <reason>