Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ linter:
- unnecessary_await_in_return
- unnecessary_brace_in_string_interps
- unnecessary_const
- unnecessary_constructor_name
- unnecessary_final
- unnecessary_getters_setters
- unnecessary_lambdas
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ import 'rules/unawaited_futures.dart';
import 'rules/unnecessary_await_in_return.dart';
import 'rules/unnecessary_brace_in_string_interps.dart';
import 'rules/unnecessary_const.dart';
import 'rules/unnecessary_constructor_name.dart';
import 'rules/unnecessary_final.dart';
import 'rules/unnecessary_getters_setters.dart';
import 'rules/unnecessary_lambdas.dart';
Expand Down Expand Up @@ -362,6 +363,7 @@ void registerLintRules({bool inTestMode = false}) {
..register(UnnecessaryAwaitInReturn())
..register(UnnecessaryBraceInStringInterps())
..register(UnnecessaryConst())
..register(UnnecessaryConstructorName())
..register(UnnecessaryFinal())
..register(UnnecessaryNew())
..register(UnnecessaryNullAwareAssignments())
Expand Down
78 changes: 78 additions & 0 deletions lib/src/rules/unnecessary_constructor_name.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';

import '../analyzer.dart';

const _desc = r'Unnecessary `.new` constructor name.';

const _details = r'''
**PREFER** using the default unnamed Constructor over `.new`.

Given a class `C`, the named unnamed constructor `C.new` refers to the same
constructor as the unnamed `C`. As such it adds nothing but visual noise to
invocations and should be avoided (unless being used to identify a constructor
tear-off).

**BAD:**
```dart
class A {
A.new(); // LINT
}

var a = A.new(); // LINT
```

**GOOD:**
```dart
class A {
A.ok();
}

var a = A();
var aa = A.ok();
var makeA = A.new;
```
''';

class UnnecessaryConstructorName extends LintRule {
UnnecessaryConstructorName()
: super(
name: 'unnecessary_constructor_name',
description: _desc,
details: _details,
group: Group.style);

@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this);
registry.addConstructorDeclaration(this, visitor);
registry.addInstanceCreationExpression(this, visitor);
}
}

class _Visitor extends SimpleAstVisitor {
final LintRule rule;

_Visitor(this.rule);

@override
void visitConstructorDeclaration(ConstructorDeclaration node) {
_check(node.name);
}

@override
void visitInstanceCreationExpression(InstanceCreationExpression node) {
_check(node.constructorName.name);
}

void _check(SimpleIdentifier? name) {
if (name?.name == 'new') {
rule.reportLint(name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// test w/ `dart test -N unnecessary_constructor_name`

class A {
A.new(); // LINT
A.ok(); // OK
}

var a = A.new(); // LINT
var aa = A(); // OK
var aaa = A.ok(); // OK
var makeA = A.new; // OK