unnecessary-constructor-name

Same as unnecessary_constructor_name in Dart lints

Group
Style
Path
lint/style/unnecessary-constructor-name
Default
On (recommended)
Origin
Dart lints
Rule
453 of 512
recommended

Flags a redundant .new on constructor declarations and invocations.

X.new(...) names the default (unnamed) constructor explicitly, which is exactly equivalent to the shorter X(...), so the .new is noise. The rule catches both the plain call form (X.new(...)) and the new/const form (const X.new(...)). Declarations such as class X { X.new(); } are also reported. A bare X.new constructor tear-off that is not invoked is deliberately left alone: there the .new is required to reference the constructor as a value.

Invalid

example.dartdart
// Bad: explicit `.new` on a constructor invocation is redundant.
class Foo {
  Foo();
}

class Baz {
  Baz.new();
}

void examples() {
  final a = Foo.new();
  final b = Foo.new()..toString();
  final c = <Foo>[Foo.new()];
  final d = identical(a, Foo.new());
  final e = Foo.new();
  final g = Foo.new();
  print([a, b, c, d, e, g]);
}
Remove `.new` because the default constructor can be invoked without it.
6class Baz {
7 Baz.new();
Remove `.new` because the default constructor can be invoked without it.
10void examples() {
11 final a = Foo.new();
Remove `.new` because the default constructor can be invoked without it.
11 final a = Foo.new();
12 final b = Foo.new()..toString();
Remove `.new` because the default constructor can be invoked without it.
12 final b = Foo.new()..toString();
13 final c = <Foo>[Foo.new()];
Remove `.new` because the default constructor can be invoked without it.
13 final c = <Foo>[Foo.new()];
14 final d = identical(a, Foo.new());
Remove `.new` because the default constructor can be invoked without it.
14 final d = identical(a, Foo.new());
15 final e = Foo.new();
Remove `.new` because the default constructor can be invoked without it.
15 final e = Foo.new();
16 final g = Foo.new();

Valid

example.dartdart
// Good: no redundant `.new`. Bare `X.new` tear-offs keep `.new` (it is required there).
class Foo {
  Foo();
  Foo.named();
}

void examples() {
  final tearOff = Foo.new; // tear-off of the default constructor — `.new` required
  final a = Foo();
  final b = Foo.named();
  final list = [1, 2, 3].map((_) => Foo.new).toList();
  final c = Foo.named()..toString();
  final d = StringBuffer();
  print([tearOff, a, b, list, c, d]);
}

Configure unnecessary-constructor-name

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "unnecessary-constructor-name": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/unnecessary-constructor-name: <reason>