analyzer: Do not report duplicate errors on return expression in sync*

Fixes https://github.com/dart-lang/sdk/issues/45960

Change-Id: I49c88ed9419afb8d89bd518ca3552ec6aaa6e124
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/218980
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
diff --git a/pkg/analyzer/lib/src/error/return_type_verifier.dart b/pkg/analyzer/lib/src/error/return_type_verifier.dart
index 06ef0ed..2ae92c7 100644
--- a/pkg/analyzer/lib/src/error/return_type_verifier.dart
+++ b/pkg/analyzer/lib/src/error/return_type_verifier.dart
@@ -134,6 +134,12 @@
       return;
     }
 
+    if (enclosingExecutable.isGenerator) {
+      // [CompileTimeErrorCode.RETURN_IN_GENERATOR] has already been reported;
+      // do not report a duplicate error.
+      return;
+    }
+
     if (_typeSystem.isNonNullableByDefault) {
       _checkReturnExpression_nullSafety(expression);
     } else {
diff --git a/pkg/analyzer/test/src/diagnostics/return_of_invalid_type_test.dart b/pkg/analyzer/test/src/diagnostics/return_of_invalid_type_test.dart
index 89bab3f..ef2237d 100644
--- a/pkg/analyzer/test/src/diagnostics/return_of_invalid_type_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/return_of_invalid_type_test.dart
@@ -234,6 +234,15 @@
 ''');
   }
 
+  test_function_asyncStar() async {
+    await assertErrorsInCode('''
+Stream<int> f() async* => 3;
+''', [
+      // RETURN_OF_INVALID_TYPE shouldn't be reported in addition to this error.
+      error(CompileTimeErrorCode.RETURN_IN_GENERATOR, 23, 2),
+    ]);
+  }
+
   test_function_sync_block__to_dynamic() async {
     await assertNoErrorsInCode(r'''
 f() {
@@ -430,6 +439,15 @@
     ]);
   }
 
+  test_function_syncStar() async {
+    await assertErrorsInCode('''
+Iterable<int> f() sync* => 3;
+''', [
+      // RETURN_OF_INVALID_TYPE shouldn't be reported in addition to this error.
+      error(CompileTimeErrorCode.RETURN_IN_GENERATOR, 24, 2),
+    ]);
+  }
+
   test_getter_sync_block_String__to_int() async {
     await assertErrorsInCode('''
 int get g {