Skip to content

Commit 3ae91fc

Browse files
[nnbd_migration] Add an edge origin for return;
Change-Id: I94e56933c4fb8d8c8b80ca0c427fc34c56674d04 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/137841 Reviewed-by: Paul Berry <[email protected]>
1 parent 1d1f3a5 commit 3ae91fc

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

pkg/analysis_server/lib/src/edit/nnbd_migration/info_builder.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ class InfoBuilder {
192192

193193
if (origin.kind == EdgeOriginKind.uninitializedRead) {
194194
return 'Used on line $lineNumber, when it is possibly uninitialized';
195+
} else if (origin.kind == EdgeOriginKind.implicitNullReturn) {
196+
return 'This function contains a return statement with no value on line '
197+
'$lineNumber, which implicitly returns null.';
195198
}
196199

197200
/// If the [node] is inside the return expression for a function body,

pkg/analysis_server/test/src/edit/nnbd_migration/info_builder_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,24 @@ String? g() {
15681568
assertDetail(detail: regions[0].details[0], offset: 15, length: 6);
15691569
}
15701570

1571+
Future<void> test_returnNoValue() async {
1572+
UnitInfo unit = await buildInfoForSingleTestFile('''
1573+
int f() {
1574+
return;
1575+
}
1576+
''', migratedContent: '''
1577+
int? f() {
1578+
return;
1579+
}
1580+
''');
1581+
List<RegionInfo> regions = unit.fixRegions;
1582+
expect(regions, hasLength(1));
1583+
assertRegion(region: regions[0], offset: 3, details: [
1584+
'This function contains a return statement with no value on line 2,'
1585+
' which implicitly returns null.'
1586+
]);
1587+
}
1588+
15711589
Future<void> test_returnType_function_expression() async {
15721590
UnitInfo unit = await buildInfoForSingleTestFile('''
15731591
int _f = null;

pkg/nnbd_migration/lib/instrumentation.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ enum EdgeOriginKind {
137137
ifNull,
138138
implicitMixinSuperCall,
139139
implicitNullInitializer,
140+
implicitNullReturn,
140141
inferredTypeParameterInstantiation,
141142
initializerInference,
142143
instanceCreation,

pkg/nnbd_migration/lib/src/edge_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
12341234
offset: node.offset);
12351235
_graph.makeNullable(
12361236
implicitNullType.node, AlwaysNullableTypeOrigin(source, node));
1237-
_checkAssignment(null,
1237+
_checkAssignment(ImplicitNullReturnOrigin(source, node),
12381238
source: isAsync
12391239
? _futureOf(implicitNullType, offset: node.offset)
12401240
: implicitNullType,

pkg/nnbd_migration/lib/src/edge_origin.dart

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,29 @@ class ImplicitNullInitializerOrigin extends EdgeOrigin {
202202
EdgeOriginKind get kind => EdgeOriginKind.implicitNullInitializer;
203203
}
204204

205+
/// Edge origin resulting from a `return;` statement which implicitly returns
206+
/// `null`.
207+
class ImplicitNullReturnOrigin extends EdgeOrigin {
208+
ImplicitNullReturnOrigin(Source source, ReturnStatement node)
209+
: super(source, node);
210+
211+
@override
212+
EdgeOriginKind get kind => EdgeOriginKind.implicitNullReturn;
213+
214+
@override
215+
ReturnStatement get node => super.node as ReturnStatement;
216+
}
217+
218+
/// Edge origin resulting from the inference of a type parameter, which
219+
/// can affects the nullability of that type parameter's bound.
220+
class InferredTypeParameterInstantiationOrigin extends EdgeOrigin {
221+
InferredTypeParameterInstantiationOrigin(Source source, AstNode node)
222+
: super(source, node);
223+
224+
@override
225+
EdgeOriginKind get kind => EdgeOriginKind.inferredTypeParameterInstantiation;
226+
}
227+
205228
/// Edge origin resulting from a type that is inferred from its initializer.
206229
class InitializerInferenceOrigin extends EdgeOrigin {
207230
InitializerInferenceOrigin(Source source, VariableDeclaration node)
@@ -434,16 +457,6 @@ class TypeParameterInstantiationOrigin extends EdgeOrigin {
434457
TypeAnnotation get node => super.node as TypeAnnotation;
435458
}
436459

437-
/// Edge origin resulting from the inference of a type parameter, which
438-
/// can affects the nullability of that type parameter's bound.
439-
class InferredTypeParameterInstantiationOrigin extends EdgeOrigin {
440-
InferredTypeParameterInstantiationOrigin(Source source, AstNode node)
441-
: super(source, node);
442-
443-
@override
444-
EdgeOriginKind get kind => EdgeOriginKind.inferredTypeParameterInstantiation;
445-
}
446-
447460
/// Edge origin resulting from the read of a variable that has not been
448461
/// definitely assigned a value.
449462
class UninitializedReadOrigin extends EdgeOrigin {

0 commit comments

Comments
 (0)