Fix NPE when the return type of a method is inherited.
Change-Id: Iaf77f250c806b116f0f0d6bbd3d9b9ff2ab0090d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192540
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/make_return_type_nullable.dart b/pkg/analysis_server/lib/src/services/correction/dart/make_return_type_nullable.dart
index 5fdea07..fa90e5f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/make_return_type_nullable.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/make_return_type_nullable.dart
@@ -22,18 +22,12 @@
return;
}
var body = node.thisOrAncestorOfType<FunctionBody>();
- TypeAnnotation returnType;
- var function = body.parent;
- if (function is FunctionExpression) {
- function = function.parent;
- }
- if (function is MethodDeclaration) {
- returnType = function.returnType;
- } else if (function is FunctionDeclaration) {
- returnType = function.returnType;
- } else {
+
+ var returnType = _getReturnTypeNode(body);
+ if (returnType == null) {
return;
}
+
if (body.isAsynchronous || body.isGenerator) {
if (returnType is! NamedType) {
return null;
@@ -60,4 +54,17 @@
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static MakeReturnTypeNullable newInstance() => MakeReturnTypeNullable();
+
+ static TypeAnnotation _getReturnTypeNode(FunctionBody body) {
+ var function = body.parent;
+ if (function is FunctionExpression) {
+ function = function.parent;
+ }
+ if (function is MethodDeclaration) {
+ return function.returnType;
+ } else if (function is FunctionDeclaration) {
+ return function.returnType;
+ }
+ return null;
+ }
}
diff --git a/pkg/analysis_server/test/src/services/correction/fix/make_return_type_nullable_test.dart b/pkg/analysis_server/test/src/services/correction/fix/make_return_type_nullable_test.dart
index e661f34..7e976e7 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/make_return_type_nullable_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/make_return_type_nullable_test.dart
@@ -133,6 +133,21 @@
''');
}
+ Future<void> test_method_sync_inherited() async {
+ await resolveTestCode('''
+abstract class A {
+ String m(String? s);
+}
+
+class B extends A {
+ m(String? s) {
+ return s;
+ }
+}
+''');
+ await assertNoFix();
+ }
+
Future<void> test_returnTypeHasTypeArguments() async {
await resolveTestCode('''
List<String> f() {