unnecessary-new

Same as unnecessary_new in Dart lints

Group
Style
Path
lint/style/unnecessary-new
Default
On (recommended)
Origin
Dart lints
Rule
461 of 512
recommended

Flags an explicit new keyword in an instance-creation expression.

Since Dart 2.0 the new keyword is always optional: new Foo() and Foo() construct identically. Keeping new is pure noise and breaks visual consistency with the surrounding code, which almost always omits it. The rule only targets explicit new; an implicit Foo() parses as a call and a const Foo() keeps its const, so neither is touched. Delete the new.

Invalid

example.dartdart
// Bad: explicit `new` keyword, which is always optional.
class Foo {
  Foo();
  Foo.named();
}

void examples() {
  final a = new Foo();
  final b = new Foo.named();
  final c = new List<int>.empty();
  final d = new StringBuffer();
  final e = new Foo()..toString();
  final f = [new Foo()];
  print([a, b, c, d, e, f]);
}

// ── Reached only via the constructor-initializer walk ─────────────────────────
// Each `new` below lives inside a ConstructorInitializer (field init, super-call,
// this-call, or assert). The default walk descends into those nodes only after
// the visitor traversal-gap fix, so these diagnostics regress if it is reverted.
class Wrapper {
  Wrapper([Object? o]);
}

class InitFieldInit {
  final Object a;
  InitFieldInit() : a = new Foo();
}

class InitSuperCall extends Wrapper {
  InitSuperCall() : super(new Foo());
}

class InitThisCall {
  final Object a;
  InitThisCall() : this._(new Foo());
  InitThisCall._(this.a);
}

class InitAssert {
  InitAssert() : assert(new Foo() != null);
}
Remove the optional `new` keyword.
7void examples() {
8 final a = new Foo();
Remove the optional `new` keyword.
8 final a = new Foo();
9 final b = new Foo.named();
Remove the optional `new` keyword.
9 final b = new Foo.named();
10 final c = new List<int>.empty();
Remove the optional `new` keyword.
10 final c = new List<int>.empty();
11 final d = new StringBuffer();
Remove the optional `new` keyword.
11 final d = new StringBuffer();
12 final e = new Foo()..toString();
Remove the optional `new` keyword.
12 final e = new Foo()..toString();
13 final f = [new Foo()];
Remove the optional `new` keyword.
26 final Object a;
27 InitFieldInit() : a = new Foo();
Remove the optional `new` keyword.
30class InitSuperCall extends Wrapper {
31 InitSuperCall() : super(new Foo());
Remove the optional `new` keyword.
35 final Object a;
36 InitThisCall() : this._(new Foo());
Remove the optional `new` keyword.
40class InitAssert {
41 InitAssert() : assert(new Foo() != null);

Valid

example.dartdart
// Good: instance creation without the `new` keyword.
class Foo {
  Foo();
  Foo.named();
}

void examples() {
  final a = Foo();
  final b = Foo.named();
  final c = StringBuffer();
  final d = <int>[1, 2, 3];
  final e = <int>{};
  final f = 'a literal';
  print([a, b, c, d, e, f]);
}

Configure unnecessary-new

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

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

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

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