Rules / Style / unnecessary-constructor-name
unnecessary-constructor-name
Flags a redundant .new on constructor 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(...)).
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();
}
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]);
}
Unnecessary `.new`; the default constructor can be invoked without it.
6void examples() {
7 final a = Foo.new();
∙
Unnecessary `.new`; the default constructor can be invoked without it.
7 final a = Foo.new();
8 final b = Foo.new()..toString();
∙
Unnecessary `.new`; the default constructor can be invoked without it.
8 final b = Foo.new()..toString();
9 final c = <Foo>[Foo.new()];
∙
Unnecessary `.new`; the default constructor can be invoked without it.
9 final c = <Foo>[Foo.new()];
10 final d = identical(a, Foo.new());
∙
Unnecessary `.new`; the default constructor can be invoked without it.
10 final d = identical(a, Foo.new());
11 final e = Foo.new();
∙
Unnecessary `.new`; the default constructor can be invoked without it.
11 final e = Foo.new();
12 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]);
}
How to configure
Set the severity of unnecessary-constructor-name in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"unnecessary-constructor-name": "error"
}
}
}
}