Skip to content

Commit

Permalink
Stop passing a nullable value to Future<nn-type>.value or Completer<n…
Browse files Browse the repository at this point in the history
…n-type>.completer (#6228)

* Stop passing a nullable value to Future<nn-type>.value or Completer<nn-type>.completer.

This is cleanup work required to start enforcing this with static analysis, as per dart-lang/sdk#53253.

Real quick this issue is that this code is unsafe:

```dart
void f(Completer<int> c, int? i) {
  Future<int>.value(i); // Ouch!
  c.complete(i);        // Ouch!
}
```

The typical fix is to add a null-assert (`!`), but sometimes a more appropriate or safer fix can be made.

* Use non-nullable CpuSamples
  • Loading branch information
srawlins authored Aug 21, 2023
1 parent 51b601e commit c2e0343
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ class _ScriptCache {
ScriptRef scriptRef,
) {
final scriptId = scriptRef.id!;
if (_scripts.containsKey(scriptId)) {
return Future.value(_scripts[scriptId]);
final script = _scripts[scriptId];
if (script != null) {
return Future.value(script);
}

if (_inProgress.containsKey(scriptId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class FakeServiceManager extends Fake implements ServiceConnectionManager {
Response.parse({
'layerBytes': 0,
'pictureBytes': 0,
}),
})!,
);

@override
Expand Down Expand Up @@ -192,7 +192,7 @@ class FakeServiceManager extends Fake implements ServiceConnectionManager {
'dartSdkVersion': '2.9.0 (build 2.9.0-8.0.dev d6fed1f624)',
'frameworkRevisionShort': '74432fa91c',
'engineRevisionShort': 'ae2222f47e',
}),
})!,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class FakeVmServiceWrapper extends Fake implements VmServiceWrapper {
'pid': 54321,
'functions': [],
'samples': [],
});
})!;

CpuSamples? cpuSamples;
CpuSamples cpuSamples;

CpuSamples? allocationSamples;

Expand Down Expand Up @@ -249,7 +249,7 @@ class FakeVmServiceWrapper extends Fake implements VmServiceWrapper {
},
),
},
),
)!,
);
}

Expand Down Expand Up @@ -338,7 +338,7 @@ class FakeVmServiceWrapper extends Fake implements VmServiceWrapper {

@override
Future<TimelineFlags> getVMTimelineFlags() =>
Future.value(TimelineFlags.parse(_vmTimelineFlags));
Future.value(TimelineFlags.parse(_vmTimelineFlags)!);

@override
Future<Timeline> getVMTimeline({
Expand Down Expand Up @@ -403,7 +403,7 @@ class FakeVmServiceWrapper extends Fake implements VmServiceWrapper {
) async {
final httpProfile = await getHttpProfile(isolateId);
return Future.value(
httpProfile.requests.firstWhereOrNull((request) => request.id == id),
httpProfile.requests.firstWhere((request) => request.id == id),
);
}

Expand Down

0 comments on commit c2e0343

Please sign in to comment.