Skip to content

Commit 23b360b

Browse files
srawlinsCommit Queue
authored andcommitted
linter: prefer_ctors: dont report for subtypes
Fixes https://github.com/dart-lang/linter/issues/3821 Change-Id: I1cd5de00a42fc0d7a109a6bffb22f4af6be1854e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/364967 Auto-Submit: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 582e303 commit 23b360b

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

pkg/linter/lib/src/rules/prefer_constructors_over_static_methods.dart

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import 'package:analyzer/dart/element/type.dart';
99
import '../analyzer.dart';
1010

1111
const _desc =
12-
r'Prefer defining constructors instead of static methods to create instances.';
12+
r'Prefer defining constructors instead of static methods to create '
13+
'instances.';
1314

1415
const _details = r'''
1516
**PREFER** defining constructors instead of static methods to create instances.
@@ -41,11 +42,6 @@ class Point {
4142
```
4243
''';
4344

44-
// TODO(pq): temporary; remove after renamed class is in the SDK
45-
// ignore: non_constant_identifier_names
46-
LintRule PreferConstructorsInsteadOfStaticMethods() =>
47-
PreferConstructorsOverStaticMethods();
48-
4945
bool _hasNewInvocation(DartType returnType, FunctionBody body) =>
5046
_BodyVisitor(returnType).containsInstanceCreation(body);
5147

@@ -68,7 +64,7 @@ class PreferConstructorsOverStaticMethods extends LintRule {
6864
@override
6965
void registerNodeProcessors(
7066
NodeLintRegistry registry, LinterContext context) {
71-
var visitor = _Visitor(this, context);
67+
var visitor = _Visitor(this);
7268
registry.addMethodDeclaration(this, visitor);
7369
}
7470
}
@@ -86,6 +82,9 @@ class _BodyVisitor extends RecursiveAstVisitor {
8682

8783
@override
8884
visitInstanceCreationExpression(InstanceCreationExpression node) {
85+
// TODO(srawlins): This assignment overrides existing `found` values.
86+
// For example, given `() { C(); D(); }`, if `C` was the return type being
87+
// sought, then the `found` value is overridden when we visit `D()`.
8988
found = node.staticType == returnType;
9089
if (!found) {
9190
super.visitInstanceCreationExpression(node);
@@ -95,9 +94,8 @@ class _BodyVisitor extends RecursiveAstVisitor {
9594

9695
class _Visitor extends SimpleAstVisitor<void> {
9796
final LintRule rule;
98-
final LinterContext context;
9997

100-
_Visitor(this.rule, this.context);
98+
_Visitor(this.rule);
10199

102100
@override
103101
void visitMethodDeclaration(MethodDeclaration node) {
@@ -107,13 +105,10 @@ class _Visitor extends SimpleAstVisitor<void> {
107105
if (returnType is! InterfaceType) return;
108106

109107
var interfaceType = node.parent.typeToCheckOrNull();
110-
if (interfaceType != null) {
111-
if (!context.typeSystem.isAssignableTo(returnType, interfaceType)) {
112-
return;
113-
}
114-
if (_hasNewInvocation(returnType, node.body)) {
115-
rule.reportLintForToken(node.name);
116-
}
108+
if (interfaceType != returnType) return;
109+
110+
if (_hasNewInvocation(returnType, node.body)) {
111+
rule.reportLintForToken(node.name);
117112
}
118113
}
119114
}

pkg/linter/test/rules/prefer_constructors_over_static_methods_test.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class A {
6262
}
6363

6464
test_staticMethod_expressionBody_extensionType() async {
65-
// Since the check logic is shared one test should be sufficient to verify
65+
// Since the check logic is shared, one test should be sufficient to verify
6666
// extension types are supported.
6767
await assertDiagnostics(r'''
6868
extension type E(int i) {
@@ -125,13 +125,19 @@ class A {
125125
''');
126126
}
127127

128+
test_staticMethod_returnsSubtype() async {
129+
await assertNoDiagnostics(r'''
130+
class A {
131+
static B f() => B();
132+
}
133+
class B extends A {}
134+
''');
135+
}
136+
128137
test_staticMethod_returnsUnrelatedType() async {
129138
await assertNoDiagnostics(r'''
130139
class A {
131-
A.named();
132140
static Object staticM() => Object();
133-
134-
/*static A? ok2() => 1==1 ? null : A.internal(); // OK*/
135141
}
136142
''');
137143
}

0 commit comments

Comments
 (0)