Fix `UseResult` in `AsExpression`
Fixes #48879
Change-Id: Ib43b72f96d05a119f37e18ca47bf71d2d0ebc52c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/242287
Reviewed-by: Ömer Ağacan <omersa@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/error/use_result_verifier.dart b/pkg/analyzer/lib/src/error/use_result_verifier.dart
index a54515c..539e971 100644
--- a/pkg/analyzer/lib/src/error/use_result_verifier.dart
+++ b/pkg/analyzer/lib/src/error/use_result_verifier.dart
@@ -168,7 +168,8 @@
}
}
- if (parent is AwaitExpression ||
+ if (parent is AsExpression ||
+ parent is AwaitExpression ||
parent is ConditionalExpression ||
parent is ForElement ||
parent is IfElement ||
diff --git a/pkg/analyzer/test/src/diagnostics/unused_result_test.dart b/pkg/analyzer/test/src/diagnostics/unused_result_test.dart
index 1954c47..e7583c4 100644
--- a/pkg/analyzer/test/src/diagnostics/unused_result_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/unused_result_test.dart
@@ -21,6 +21,44 @@
writeTestPackageConfigWithMeta();
}
+ test_as() async {
+ await assertNoErrorsInCode(r'''
+import 'package:meta/meta.dart';
+
+class A {}
+class B extends A {}
+
+B createB() {
+ return createA() as B;
+}
+
+@UseResult('')
+A createA() {
+ return B();
+}
+''');
+ }
+
+ test_as_without_usage() async {
+ await assertErrorsInCode(r'''
+import 'package:meta/meta.dart';
+
+class A {}
+class B extends A {}
+
+void test() {
+ createA() as B;
+}
+
+@UseResult('')
+A createA() {
+ return B();
+}
+''', [
+ error(HintCode.UNUSED_RESULT, 83, 7),
+ ]);
+ }
+
test_field_result_assigned() async {
await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';