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
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
// 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]);
}class Baz { Baz.new();void examples() { final a = Foo.new(); final a = Foo.new(); final b = Foo.new()..toString(); final b = Foo.new()..toString(); final c = <Foo>[Foo.new()]; final c = <Foo>[Foo.new()]; final d = identical(a, Foo.new()); final d = identical(a, Foo.new()); final e = Foo.new(); final e = Foo.new(); final g = Foo.new();Valid
// 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.
{
"linter": {
"rules": {
"style": {
"unnecessary-constructor-name": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/unnecessary-constructor-name: <reason>More style rules
- unnecessary-final
unnecessary_finalDon't use
finalfor local variables and parameters. - unnecessary-late
unnecessary_laterecommendedFlags a redundant
latemodifier on an initialized static or top-level variable. - unnecessary-library-directive
unnecessary_library_directiverecommendedFlags a
librarydirective that has neither a doc comment nor annotations. - unnecessary-library-name
unnecessary_library_namerecommendedFlags a
librarydirective that carries an explicit name. - unnecessary-new
unnecessary_newrecommendedFlags an explicit
newkeyword in an instance-creation expression. - unnecessary-null-aware-operator-on-extension-on-nullable
unnecessary_null_aware_operator_on_extension_on_nullableFlags a null-aware access whose member comes from an extension on a nullable type.
- unnecessary-null-checks
unnecessary_null_checksFlags a null assertion
!on a value that flows into a nullable context. - unnecessary-nullable-for-final-variable-declarations
unnecessary_nullable_for_final_variable_declarationsrecommendedFlags a
finalorconstvariable given a nullable type but initialized to a provably non-null value.