diff --git a/pkg/analyzer/lib/src/test_utilities/mock_packages.dart b/pkg/analyzer/lib/src/test_utilities/mock_packages.dart index f2488142508a..8ec3329a51ea 100644 --- a/pkg/analyzer/lib/src/test_utilities/mock_packages.dart +++ b/pkg/analyzer/lib/src/test_utilities/mock_packages.dart @@ -194,10 +194,12 @@ class _Checked { @Target({ TargetKind.classType, + TargetKind.constructor, TargetKind.function, TargetKind.getter, TargetKind.library, TargetKind.method, + TargetKind.mixinType, }) class _DoNotStore { const _DoNotStore(); diff --git a/pkg/analyzer/test/src/diagnostics/assignment_of_do_not_store_test.dart b/pkg/analyzer/test/src/diagnostics/assignment_of_do_not_store_test.dart index 138d53899baf..bb6dd945e0c4 100644 --- a/pkg/analyzer/test/src/diagnostics/assignment_of_do_not_store_test.dart +++ b/pkg/analyzer/test/src/diagnostics/assignment_of_do_not_store_test.dart @@ -55,6 +55,62 @@ class AssignmentOfDoNotStoreTest extends PubPackageResolutionTest { writeTestPackageConfigWithMeta(); } + test_class_containingInstanceGetter() async { + await assertErrorsInCode(''' +import 'package:meta/meta.dart'; +@doNotStore +class A { + String get v => ''; +} + +String f = A().v; +''', [ + error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 91, 5), + ]); + } + + test_class_containingInstanceMethod() async { + await assertErrorsInCode(''' +import 'package:meta/meta.dart'; +@doNotStore +class A { + String v() => ''; +} + +String f = A().v(); +''', [ + error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 89, 7), + ]); + } + + test_class_containingStaticGetter() async { + await assertErrorsInCode(''' +import 'package:meta/meta.dart'; +@doNotStore +class A { + static String get v => ''; +} + +String f = A.v; +''', [ + error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 98, 3), + ]); + } + + test_class_containingStaticMethod() async { + await assertErrorsInCode(''' +import 'package:meta/meta.dart'; +@doNotStore +class A { + static String v() => ''; +} + +String f = A.v(); +''', [ + error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 96, 5), + ]); + } + test_classMemberGetter() async { await assertErrorsInCode(''' import 'package:meta/meta.dart'; @@ -171,6 +227,23 @@ class B { ]); } + test_mixin_containingInstanceMethod() async { + await assertErrorsInCode(''' +import 'package:meta/meta.dart'; +@doNotStore +mixin M { + String v() => ''; +} + +abstract class A { + M get m; + late String f = m.v(); +} +''', [ + error(WarningCode.ASSIGNMENT_OF_DO_NOT_STORE, 126, 5), + ]); + } + test_tearOff() async { await assertNoErrorsInCode(''' import 'package:meta/meta.dart'; diff --git a/pkg/analyzer/test/src/diagnostics/return_of_do_not_store_test.dart b/pkg/analyzer/test/src/diagnostics/return_of_do_not_store_test.dart index 9c85f4f9ce68..58a4b8c35ea2 100644 --- a/pkg/analyzer/test/src/diagnostics/return_of_do_not_store_test.dart +++ b/pkg/analyzer/test/src/diagnostics/return_of_do_not_store_test.dart @@ -59,6 +59,23 @@ class ReturnOfDoNotStoreTest extends PubPackageResolutionTest { writeTestPackageConfigWithMeta(); } + test_constructor() async { + await assertErrorsInCode(''' +import 'package:meta/meta.dart'; + +class A { + @doNotStore + A(); + + String getA() { + return A(); + } +} +''', [ + error(CompileTimeErrorCode.RETURN_OF_INVALID_TYPE_FROM_METHOD, 95, 3), + ]); + } + @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/48476') test_returnFromClosureInFunction() async { await assertErrorsInCode(''' diff --git a/pkg/analyzer_utilities/lib/test/mock_packages/package_content/meta/lib/meta.dart b/pkg/analyzer_utilities/lib/test/mock_packages/package_content/meta/lib/meta.dart index f7fe16a71205..025a6396245d 100644 --- a/pkg/analyzer_utilities/lib/test/mock_packages/package_content/meta/lib/meta.dart +++ b/pkg/analyzer_utilities/lib/test/mock_packages/package_content/meta/lib/meta.dart @@ -71,14 +71,14 @@ const _AlwaysThrows alwaysThrows = _AlwaysThrows(); @Deprecated('Use the `covariant` modifier instead') const _Checked checked = _Checked(); -/// Used to annotate a method, getter or top-level getter or function to -/// indicate that the value obtained by invoking it should not be stored in a +/// Used to annotate a method, getter, top-level function, or top-level getter +/// to indicate that the value obtained by invoking it should not be stored in a /// field or top-level variable. The annotation can also be applied to a class /// to implicitly annotate all of the valid members of the class, or applied to /// a library to annotate all of the valid members of the library, including -/// classes. If a value returned by an element marked as `doNotStore` is returned -/// from a function or getter, that function or getter should be similarly -/// annotated. +/// classes. If a value returned by an element marked as `doNotStore` is +/// returned from a function or getter, that function or getter should be +/// similarly annotated. /// /// Tools, such as the analyzer, can provide feedback if /// @@ -90,9 +90,9 @@ const _Checked checked = _Checked(); /// or top-level variable. const _DoNotStore doNotStore = _DoNotStore(); -/// Used to annotate a method, getter or top-level getter or function that is -/// not intended to be accessed in checked-in code, but might be ephemerally -/// used during development or local testing. +/// Used to annotate a method, getter, top-level function, or top-level getter +/// that is not intended to be accessed in checked-in code, but might be +/// ephemerally used during development or local testing. /// /// The intention of this annotation is to signify an API is available for /// temporary or ephemeral use (such as debugging or local testing), but should @@ -610,12 +610,12 @@ class _Checked { @Target({ TargetKind.classType, - // TODO(srawlins): Add `TargetKind.constructor` when this annotation has - // functional tests. See https://github.com/dart-lang/sdk/issues/48476. + TargetKind.constructor, TargetKind.function, TargetKind.getter, TargetKind.library, TargetKind.method, + TargetKind.mixinType, }) class _DoNotStore { const _DoNotStore(); diff --git a/pkg/meta/lib/meta.dart b/pkg/meta/lib/meta.dart index e3292b78cccd..385eefdfce99 100644 --- a/pkg/meta/lib/meta.dart +++ b/pkg/meta/lib/meta.dart @@ -71,14 +71,14 @@ const _AlwaysThrows alwaysThrows = _AlwaysThrows(); @Deprecated('Use the `covariant` modifier instead') const _Checked checked = _Checked(); -/// Used to annotate a method, getter or top-level getter or function to -/// indicate that the value obtained by invoking it should not be stored in a +/// Used to annotate a method, getter, top-level function, or top-level getter +/// to indicate that the value obtained by invoking it should not be stored in a /// field or top-level variable. The annotation can also be applied to a class /// to implicitly annotate all of the valid members of the class, or applied to /// a library to annotate all of the valid members of the library, including -/// classes. If a value returned by an element marked as `doNotStore` is returned -/// from a function or getter, that function or getter should be similarly -/// annotated. +/// classes. If a value returned by an element marked as `doNotStore` is +/// returned from a function or getter, that function or getter should be +/// similarly annotated. /// /// Tools, such as the analyzer, can provide feedback if /// @@ -90,9 +90,9 @@ const _Checked checked = _Checked(); /// or top-level variable. const _DoNotStore doNotStore = _DoNotStore(); -/// Used to annotate a method, getter or top-level getter or function that is -/// not intended to be accessed in checked-in code, but might be ephemerally -/// used during development or local testing. +/// Used to annotate a method, getter, top-level function, or top-level getter +/// that is not intended to be accessed in checked-in code, but might be +/// ephemerally used during development or local testing. /// /// The intention of this annotation is to signify an API is available for /// temporary or ephemeral use (such as debugging or local testing), but should @@ -641,12 +641,12 @@ class _Checked { @Target({ TargetKind.classType, - // TODO(srawlins): Add `TargetKind.constructor` when this annotation has - // functional tests. See https://github.com/dart-lang/sdk/issues/48476. + TargetKind.constructor, TargetKind.function, TargetKind.getter, TargetKind.library, TargetKind.method, + TargetKind.mixinType, }) class _DoNotStore { const _DoNotStore();