Skip to content

Commit 89380c5

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Guard DotShorthandConstructorInvocation from Deprecated.instantiate
DotShorthandConstructorInvocation does not have a ConstructorName, so we convert `DeprecatedFunctionalityVerifier.constructorName` into `._checkForDeprecatedInstantiate`, and then call that in `constructorName`, `dotShorthandConstructorInvocation` and `instanceCreationExpression`. Change-Id: I04d0aea70afce94fe2e574c38e02bbd5dbb7933f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/451661 Reviewed-by: Kallen Tu <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent f5fedc4 commit 89380c5

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

pkg/analyzer/lib/src/error/deprecated_functionality_verifier.dart

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,25 @@ class DeprecatedFunctionalityVerifier {
3131
}
3232

3333
void constructorName(ConstructorName node) {
34-
var classElement = node.type.element;
35-
if (classElement == null) return;
36-
if (classElement.isDeprecatedWithKind('instantiate')) {
37-
_diagnosticReporter.atNode(
38-
node,
39-
WarningCode.deprecatedInstantiate,
40-
arguments: [classElement.name!],
41-
);
42-
}
34+
var interfaceElement = node.type.element;
35+
if (interfaceElement is! InterfaceElement) return;
36+
_checkForDeprecatedInstantiate(element: interfaceElement, errorNode: node);
4337
}
4438

4539
void dotShorthandConstructorInvocation(
4640
DotShorthandConstructorInvocation node,
4741
) {
48-
var element = node.constructorName.element;
49-
if (element is! ExecutableElement) return;
42+
var element = node.element;
43+
if (element is! ConstructorElement) return;
5044
_checkForDeprecatedOptional(
5145
element: element,
5246
argumentList: node.argumentList,
5347
errorNode: node.constructorName,
5448
);
49+
_checkForDeprecatedInstantiate(
50+
element: element.enclosingElement,
51+
errorNode: node.constructorName,
52+
);
5553
}
5654

5755
void dotShorthandInvocation(DotShorthandInvocation node) {
@@ -77,6 +75,12 @@ class DeprecatedFunctionalityVerifier {
7775
argumentList: node.argumentList,
7876
errorNode: node.constructorName,
7977
);
78+
var interfaceElement = node.constructorName.type.element;
79+
if (interfaceElement is! InterfaceElement) return;
80+
_checkForDeprecatedInstantiate(
81+
element: interfaceElement,
82+
errorNode: node.constructorName,
83+
);
8084
}
8185

8286
void methodInvocation(MethodInvocation node) {
@@ -143,6 +147,19 @@ class DeprecatedFunctionalityVerifier {
143147
}
144148
}
145149

150+
void _checkForDeprecatedInstantiate({
151+
required InterfaceElement element,
152+
required AstNode errorNode,
153+
}) {
154+
if (element.isDeprecatedWithKind('instantiate')) {
155+
_diagnosticReporter.atNode(
156+
errorNode,
157+
WarningCode.deprecatedInstantiate,
158+
arguments: [element.name!],
159+
);
160+
}
161+
}
162+
146163
void _checkForDeprecatedMixin(WithClause? node) {
147164
if (node == null) return;
148165
for (var mixin in node.mixinTypes) {

pkg/analyzer/test/src/diagnostics/deprecated_instantiate_test.dart

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,36 @@ var x = Foo();
3030
);
3131
}
3232

33+
test_annotatedClass_dotShorthand() async {
34+
newFile('$testPackageLibPath/foo.dart', r'''
35+
@Deprecated.instantiate()
36+
class Foo {}
37+
''');
38+
await assertErrorsInCode(
39+
r'''
40+
import 'foo.dart';
41+
Foo x = .new();
42+
''',
43+
[error(WarningCode.deprecatedInstantiate, 28, 3)],
44+
);
45+
}
46+
47+
test_annotatedClass_dotShorthand_named() async {
48+
newFile('$testPackageLibPath/foo.dart', r'''
49+
@Deprecated.instantiate()
50+
class Foo {
51+
Foo.named();
52+
}
53+
''');
54+
await assertErrorsInCode(
55+
r'''
56+
import 'foo.dart';
57+
Foo x = .named();
58+
''',
59+
[error(WarningCode.deprecatedInstantiate, 28, 5)],
60+
);
61+
}
62+
3363
test_annotatedClass_redirectedFactory_named() async {
3464
newFile('$testPackageLibPath/foo.dart', r'''
3565
import 'test.dart';
@@ -93,7 +123,6 @@ class Bar extends Foo {
93123
@Deprecated.instantiate()
94124
class Foo {}
95125
''');
96-
97126
await assertNoErrorsInCode(r'''
98127
import 'foo.dart';
99128
class Bar extends Foo {
@@ -130,6 +159,22 @@ var x = Foo2();
130159
''');
131160
}
132161

162+
test_annotatedClass_typedef_dotShorthand() async {
163+
newFile('$testPackageLibPath/foo.dart', r'''
164+
@Deprecated.instantiate()
165+
class Foo {}
166+
typedef Foo2 = Foo;
167+
''');
168+
169+
await assertErrorsInCode(
170+
r'''
171+
import 'foo.dart';
172+
Foo2 x = .new();
173+
''',
174+
[error(WarningCode.deprecatedInstantiate, 29, 3)],
175+
);
176+
}
177+
133178
test_annotatedClass_typedef_tearoff() async {
134179
newFile('$testPackageLibPath/foo.dart', r'''
135180
@Deprecated.instantiate()

0 commit comments

Comments
 (0)