Rules / Style / unnecessary-new
unnecessary-new
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]);
}
Unnecessary `new` keyword; it is optional and can be removed.
7void examples() {
8 final a = new Foo();
∙
Unnecessary `new` keyword; it is optional and can be removed.
8 final a = new Foo();
9 final b = new Foo.named();
∙
Unnecessary `new` keyword; it is optional and can be removed.
9 final b = new Foo.named();
10 final c = new List<int>.empty();
∙
Unnecessary `new` keyword; it is optional and can be removed.
10 final c = new List<int>.empty();
11 final d = new StringBuffer();
∙
Unnecessary `new` keyword; it is optional and can be removed.
11 final d = new StringBuffer();
12 final e = new Foo()..toString();
∙
Unnecessary `new` keyword; it is optional and can be removed.
12 final e = new Foo()..toString();
13 final f = [new Foo()];
∙
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]);
}
How to configure
Set the severity of unnecessary-new in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"unnecessary-new": "error"
}
}
}
}