Skip to content

Commit 0d5830e

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
meta: Support @doNotStore on constructors and mixins
There were no tests of this component of the annotation's description: > The annotation can also be applied to a class to implicitly > annotate all of the valid members of the class So I add those as well. Bug: #48476 Change-Id: If5f0f4c6057f57b4dfd01d8f648110d69fbc5eb4 Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try,pkg-win-release-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/367880 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 798cbcf commit 0d5830e

File tree

5 files changed

+112
-20
lines changed

5 files changed

+112
-20
lines changed

pkg/analyzer/lib/src/test_utilities/mock_packages.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,12 @@ class _Checked {
194194
195195
@Target({
196196
TargetKind.classType,
197+
TargetKind.constructor,
197198
TargetKind.function,
198199
TargetKind.getter,
199200
TargetKind.library,
200201
TargetKind.method,
202+
TargetKind.mixinType,
201203
})
202204
class _DoNotStore {
203205
const _DoNotStore();

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,62 @@ class AssignmentOfDoNotStoreTest extends PubPackageResolutionTest {
5555
writeTestPackageConfigWithMeta();
5656
}
5757

58+
test_class_containingInstanceGetter() async {
59+
await assertErrorsInCode('''
60+
import 'package:meta/meta.dart';
61+
@doNotStore
62+
class A {
63+
String get v => '';
64+
}
65+
66+
String f = A().v;
67+
''', [
68+
error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 91, 5),
69+
]);
70+
}
71+
72+
test_class_containingInstanceMethod() async {
73+
await assertErrorsInCode('''
74+
import 'package:meta/meta.dart';
75+
@doNotStore
76+
class A {
77+
String v() => '';
78+
}
79+
80+
String f = A().v();
81+
''', [
82+
error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 89, 7),
83+
]);
84+
}
85+
86+
test_class_containingStaticGetter() async {
87+
await assertErrorsInCode('''
88+
import 'package:meta/meta.dart';
89+
@doNotStore
90+
class A {
91+
static String get v => '';
92+
}
93+
94+
String f = A.v;
95+
''', [
96+
error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 98, 3),
97+
]);
98+
}
99+
100+
test_class_containingStaticMethod() async {
101+
await assertErrorsInCode('''
102+
import 'package:meta/meta.dart';
103+
@doNotStore
104+
class A {
105+
static String v() => '';
106+
}
107+
108+
String f = A.v();
109+
''', [
110+
error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 96, 5),
111+
]);
112+
}
113+
58114
test_classMemberGetter() async {
59115
await assertErrorsInCode('''
60116
import 'package:meta/meta.dart';
@@ -171,6 +227,23 @@ class B {
171227
]);
172228
}
173229

230+
test_mixin_containingInstanceMethod() async {
231+
await assertErrorsInCode('''
232+
import 'package:meta/meta.dart';
233+
@doNotStore
234+
mixin M {
235+
String v() => '';
236+
}
237+
238+
abstract class A {
239+
M get m;
240+
late String f = m.v();
241+
}
242+
''', [
243+
error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 126, 5),
244+
]);
245+
}
246+
174247
test_tearOff() async {
175248
await assertNoErrorsInCode('''
176249
import 'package:meta/meta.dart';

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ class ReturnOfDoNotStoreTest extends PubPackageResolutionTest {
5959
writeTestPackageConfigWithMeta();
6060
}
6161

62+
test_constructor() async {
63+
await assertErrorsInCode('''
64+
import 'package:meta/meta.dart';
65+
66+
class A {
67+
@doNotStore
68+
A();
69+
70+
String getA() {
71+
return A();
72+
}
73+
}
74+
''', [
75+
error(CompileTimeErrorCode.RETURN_OF_INVALID_TYPE_FROM_METHOD, 95, 3),
76+
]);
77+
}
78+
6279
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/48476')
6380
test_returnFromClosureInFunction() async {
6481
await assertErrorsInCode('''

pkg/analyzer_utilities/lib/test/mock_packages/package_content/meta/lib/meta.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ const _AlwaysThrows alwaysThrows = _AlwaysThrows();
7171
@Deprecated('Use the `covariant` modifier instead')
7272
const _Checked checked = _Checked();
7373

74-
/// Used to annotate a method, getter or top-level getter or function to
75-
/// indicate that the value obtained by invoking it should not be stored in a
74+
/// Used to annotate a method, getter, top-level function, or top-level getter
75+
/// to indicate that the value obtained by invoking it should not be stored in a
7676
/// field or top-level variable. The annotation can also be applied to a class
7777
/// to implicitly annotate all of the valid members of the class, or applied to
7878
/// a library to annotate all of the valid members of the library, including
79-
/// classes. If a value returned by an element marked as `doNotStore` is returned
80-
/// from a function or getter, that function or getter should be similarly
81-
/// annotated.
79+
/// classes. If a value returned by an element marked as `doNotStore` is
80+
/// returned from a function or getter, that function or getter should be
81+
/// similarly annotated.
8282
///
8383
/// Tools, such as the analyzer, can provide feedback if
8484
///
@@ -90,9 +90,9 @@ const _Checked checked = _Checked();
9090
/// or top-level variable.
9191
const _DoNotStore doNotStore = _DoNotStore();
9292

93-
/// Used to annotate a method, getter or top-level getter or function that is
94-
/// not intended to be accessed in checked-in code, but might be ephemerally
95-
/// used during development or local testing.
93+
/// Used to annotate a method, getter, top-level function, or top-level getter
94+
/// that is not intended to be accessed in checked-in code, but might be
95+
/// ephemerally used during development or local testing.
9696
///
9797
/// The intention of this annotation is to signify an API is available for
9898
/// temporary or ephemeral use (such as debugging or local testing), but should
@@ -610,12 +610,12 @@ class _Checked {
610610

611611
@Target({
612612
TargetKind.classType,
613-
// TODO(srawlins): Add `TargetKind.constructor` when this annotation has
614-
// functional tests. See https://github.com/dart-lang/sdk/issues/48476.
613+
TargetKind.constructor,
615614
TargetKind.function,
616615
TargetKind.getter,
617616
TargetKind.library,
618617
TargetKind.method,
618+
TargetKind.mixinType,
619619
})
620620
class _DoNotStore {
621621
const _DoNotStore();

pkg/meta/lib/meta.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ const _AlwaysThrows alwaysThrows = _AlwaysThrows();
7171
@Deprecated('Use the `covariant` modifier instead')
7272
const _Checked checked = _Checked();
7373

74-
/// Used to annotate a method, getter or top-level getter or function to
75-
/// indicate that the value obtained by invoking it should not be stored in a
74+
/// Used to annotate a method, getter, top-level function, or top-level getter
75+
/// to indicate that the value obtained by invoking it should not be stored in a
7676
/// field or top-level variable. The annotation can also be applied to a class
7777
/// to implicitly annotate all of the valid members of the class, or applied to
7878
/// a library to annotate all of the valid members of the library, including
79-
/// classes. If a value returned by an element marked as `doNotStore` is returned
80-
/// from a function or getter, that function or getter should be similarly
81-
/// annotated.
79+
/// classes. If a value returned by an element marked as `doNotStore` is
80+
/// returned from a function or getter, that function or getter should be
81+
/// similarly annotated.
8282
///
8383
/// Tools, such as the analyzer, can provide feedback if
8484
///
@@ -90,9 +90,9 @@ const _Checked checked = _Checked();
9090
/// or top-level variable.
9191
const _DoNotStore doNotStore = _DoNotStore();
9292

93-
/// Used to annotate a method, getter or top-level getter or function that is
94-
/// not intended to be accessed in checked-in code, but might be ephemerally
95-
/// used during development or local testing.
93+
/// Used to annotate a method, getter, top-level function, or top-level getter
94+
/// that is not intended to be accessed in checked-in code, but might be
95+
/// ephemerally used during development or local testing.
9696
///
9797
/// The intention of this annotation is to signify an API is available for
9898
/// temporary or ephemeral use (such as debugging or local testing), but should
@@ -641,12 +641,12 @@ class _Checked {
641641

642642
@Target({
643643
TargetKind.classType,
644-
// TODO(srawlins): Add `TargetKind.constructor` when this annotation has
645-
// functional tests. See https://github.com/dart-lang/sdk/issues/48476.
644+
TargetKind.constructor,
646645
TargetKind.function,
647646
TargetKind.getter,
648647
TargetKind.library,
649648
TargetKind.method,
649+
TargetKind.mixinType,
650650
})
651651
class _DoNotStore {
652652
const _DoNotStore();

0 commit comments

Comments
 (0)