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
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
// 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);
}void examples() { final a = new Foo(); final a = new Foo(); final b = new Foo.named(); final b = new Foo.named(); final c = new List<int>.empty(); final c = new List<int>.empty(); final d = new StringBuffer(); final d = new StringBuffer(); final e = new Foo()..toString(); final e = new Foo()..toString(); final f = [new Foo()]; final Object a; InitFieldInit() : a = new Foo();class InitSuperCall extends Wrapper { InitSuperCall() : super(new Foo()); final Object a; InitThisCall() : this._(new Foo());class InitAssert { InitAssert() : assert(new Foo() != null);Valid
// 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.
{
"linter": {
"rules": {
"style": {
"unnecessary-new": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/unnecessary-new: <reason>More style rules
- 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. - unnecessary-parenthesis
unnecessary_parenthesisrecommendedFlags parentheses that can be removed without changing the meaning or hurting readability.
- unnecessary-raw-strings
unnecessary_raw_stringsrecommendedFlags raw string literals that contain no backslash or
$. - unnecessary-string-escapes
unnecessary_string_escapesrecommendedFlags backslashes that do not affect a string's value.
- unnecessary-string-interpolations
unnecessary_string_interpolationsrecommendedFlags a string literal whose entire content is a single interpolation of a provably non-nullable
String. - unnecessary-this
unnecessary_thisrecommendedFlags a redundant
this.qualifier on member access.