Skip to content

Commit 745f55f

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: ban deprecated-functionality annotations on private declarations.
In each case, the functionality which is presumably being deprecated is already non-existing (like extending a class outside of its library). So in each case we should report the annotation as nonsensical. Change-Id: Idbe36f543dfe76da4f1f8da3eaaf76bb97a58634 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/448241 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 4bc01d4 commit 745f55f

6 files changed

+107
-7
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ class AnnotationVerifier {
144144
}
145145

146146
if (declaredElement is ClassElement) {
147-
if (declaredElement.isExtendableOutside &&
147+
if (declaredElement.isPublic &&
148+
declaredElement.isExtendableOutside &&
148149
declaredElement.hasGenerativeConstructor) {
149150
return;
150151
}
@@ -169,9 +170,11 @@ class AnnotationVerifier {
169170
}
170171

171172
if (declaredElement is ClassElement &&
173+
declaredElement.isPublic &&
172174
declaredElement.isImplementableOutside) {
173175
return;
174176
} else if (declaredElement is MixinElement &&
177+
declaredElement.isPublic &&
175178
declaredElement.isImplementableOutside) {
176179
return;
177180
}
@@ -192,11 +195,11 @@ class AnnotationVerifier {
192195
declaredElement = parent.type.type?.element;
193196
}
194197

195-
if (declaredElement is ClassElement) {
196-
if (!declaredElement.isAbstract &&
197-
declaredElement.hasGenerativeConstructor) {
198-
return;
199-
}
198+
if (declaredElement is ClassElement &&
199+
declaredElement.isPublic &&
200+
!declaredElement.isAbstract &&
201+
declaredElement.hasGenerativeConstructor) {
202+
return;
200203
}
201204

202205
_diagnosticReporter.atNode(
@@ -206,7 +209,11 @@ class AnnotationVerifier {
206209
}
207210

208211
void _checkDeprecatedMixin(Annotation node, AstNode parent) {
209-
if (parent is ClassDeclaration && parent.mixinKeyword != null) return;
212+
if (parent is ClassDeclaration &&
213+
parent.declaredFragment!.element.isPublic &&
214+
parent.mixinKeyword != null) {
215+
return;
216+
}
210217

211218
_diagnosticReporter.atNode(
212219
node.name,
@@ -227,10 +234,12 @@ class AnnotationVerifier {
227234
}
228235

229236
if (declaredElement is ClassElement &&
237+
declaredElement.isPublic &&
230238
(declaredElement.isImplementableOutside ||
231239
declaredElement.isExtendableOutside)) {
232240
return;
233241
} else if (declaredElement is MixinElement &&
242+
declaredElement.isPublic &&
234243
declaredElement.isImplementableOutside) {
235244
return;
236245
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ class C {
5454
);
5555
}
5656

57+
test_class_private() async {
58+
await assertErrorsInCode(
59+
r'''
60+
@Deprecated.extend()
61+
class _C {}
62+
''',
63+
[
64+
error(WarningCode.invalidDeprecatedExtendAnnotation, 1, 17),
65+
error(WarningCode.unusedElement, 27, 2),
66+
],
67+
);
68+
}
69+
5770
test_class_sealed() async {
5871
await assertErrorsInCode(
5972
r'''

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ final class C {}
4343
);
4444
}
4545

46+
test_class_private() async {
47+
await assertErrorsInCode(
48+
r'''
49+
@Deprecated.implement()
50+
class _C {}
51+
''',
52+
[
53+
error(WarningCode.invalidDeprecatedImplementAnnotation, 1, 20),
54+
error(WarningCode.unusedElement, 30, 2),
55+
],
56+
);
57+
}
58+
4659
test_class_sealed() async {
4760
await assertErrorsInCode(
4861
r'''
@@ -88,6 +101,19 @@ base mixin M {}
88101
);
89102
}
90103

104+
test_mixin_private() async {
105+
await assertErrorsInCode(
106+
r'''
107+
@Deprecated.implement()
108+
mixin _M {}
109+
''',
110+
[
111+
error(WarningCode.invalidDeprecatedImplementAnnotation, 1, 20),
112+
error(WarningCode.unusedElement, 30, 2),
113+
],
114+
);
115+
}
116+
91117
test_typeAlias_forClass() async {
92118
await assertNoErrorsInCode(r'''
93119
class C {}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ abstract class C {}
3333
);
3434
}
3535

36+
test_class_private() async {
37+
await assertErrorsInCode(
38+
r'''
39+
@Deprecated.instantiate()
40+
class _C {}
41+
''',
42+
[
43+
error(WarningCode.invalidDeprecatedInstantiateAnnotation, 1, 22),
44+
error(WarningCode.unusedElement, 32, 2),
45+
],
46+
);
47+
}
48+
3649
test_class_privateConstructor() async {
3750
await assertErrorsInCode(
3851
r'''

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ mixin class C {}
2222
''');
2323
}
2424

25+
test_class_mixin_private() async {
26+
await assertErrorsInCode(
27+
r'''
28+
@Deprecated.mixin()
29+
mixin class _C {}
30+
''',
31+
[
32+
error(WarningCode.invalidDeprecatedMixinAnnotation, 1, 16),
33+
error(WarningCode.unusedElement, 32, 2),
34+
],
35+
);
36+
}
37+
2538
test_class_noMixin() async {
2639
await assertErrorsInCode(
2740
r'''

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ final class C {}
3232
);
3333
}
3434

35+
test_class_private() async {
36+
await assertErrorsInCode(
37+
r'''
38+
@Deprecated.subclass()
39+
class _C {}
40+
''',
41+
[
42+
error(WarningCode.invalidDeprecatedSubclassAnnotation, 1, 19),
43+
error(WarningCode.unusedElement, 29, 2),
44+
],
45+
);
46+
}
47+
3548
test_class_sealed() async {
3649
await assertErrorsInCode(
3750
r'''
@@ -69,6 +82,19 @@ base mixin M {}
6982
);
7083
}
7184

85+
test_mixin_private() async {
86+
await assertErrorsInCode(
87+
r'''
88+
@Deprecated.subclass()
89+
mixin _M {}
90+
''',
91+
[
92+
error(WarningCode.invalidDeprecatedSubclassAnnotation, 1, 19),
93+
error(WarningCode.unusedElement, 29, 2),
94+
],
95+
);
96+
}
97+
7298
test_typeAlias_ofClass() async {
7399
await assertNoErrorsInCode(r'''
74100
class C {}

0 commit comments

Comments
 (0)