Skip to content

Commit

Permalink
Enable and fix strict-casts analyzer mode (dart-lang#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough authored Oct 20, 2023
1 parent d3539c3 commit e85c0c0
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pkgs/leak_tracker/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include: package:flutter_lints/flutter.yaml

analyzer:
language:
#strict-casts: true # 14 issues
strict-casts: true
#strict-inference: true # 34 issues
#strict-raw-types: true # 103 issues
errors:
Expand Down
9 changes: 5 additions & 4 deletions pkgs/leak_tracker/lib/src/devtools_integration/_protocol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ Map<String, dynamic> sealEnvelope(Object message, Channel channel) {
};
}

/// Deserialize [message] into an opbejct of a right type.
Object openEnvelope(
/// Deserialize [message] into an object with the specified [T] type.
T openEnvelope<T>(
Map<String, dynamic> message,
Channel channel,
) {
final envelope = _envelopeByCode(message[_JsonFields.envelopeCode] as String);
final envelope =
_envelopeByCode<T>(message[_JsonFields.envelopeCode] as String);
assert(envelope.channel == channel);
return envelope.decode(message[_JsonFields.content]);
return envelope.decode(message[_JsonFields.content] as Map<String, Object?>);
}

/// Information necessary to serialize and deserialize an instance of type [T],
Expand Down
10 changes: 5 additions & 5 deletions pkgs/leak_tracker/lib/src/devtools_integration/delivery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class RequestToApp<T extends Object> {
RequestToApp(this.message);

RequestToApp.fromRequestParameters(Map<String, String> parameters)
: message = openEnvelope(
jsonDecode(parameters[_JsonFields.content]!),
: message = openEnvelope<T>(
jsonDecode(parameters[_JsonFields.content]!) as Map<String, Object?>,
Channel.requestToApp,
) as T;
);

Map<String, String> toRequestParameters() {
return {
Expand All @@ -39,7 +39,7 @@ class ResponseFromApp<T extends Object> {
ResponseFromApp(this.message);

ResponseFromApp.fromJson(Map<String, dynamic> json)
: this(openEnvelope(json, Channel.responseFromApp) as T);
: this(openEnvelope<T>(json, Channel.responseFromApp));

ResponseFromApp.fromServiceResponse(Response response)
: this.fromJson(response.json!);
Expand All @@ -59,7 +59,7 @@ class EventFromApp<T extends Object> {
EventFromApp(this.message);

EventFromApp.fromJson(Map<String, dynamic> json)
: this(openEnvelope(json, Channel.eventFromApp) as T);
: this(openEnvelope<T>(json, Channel.eventFromApp));

static EventFromApp? fromVmServiceEvent(Event event) {
if (event.extensionKind != memoryLeakTrackingExtensionName) return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ class ValueSampler {
ValueSampler({
required this.initialValue,
required this.samples,
required deltaAvg,
required double deltaAvg,
required this.deltaMax,
required absAvg,
required double absAvg,
required this.absMax,
}) : _sealed = true,
_absSum = absAvg * samples,
Expand Down
16 changes: 9 additions & 7 deletions pkgs/leak_tracker/lib/src/shared/shared_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class LeakSummary {

factory LeakSummary.fromJson(Map<String, dynamic> json) => LeakSummary(
(json[_JsonFields.totals] as Map<String, dynamic>).map(
(key, value) => MapEntry(LeakType.byName(key), int.parse(value)),
(key, value) => MapEntry(
LeakType.byName(key),
int.parse(value as String),
),
),
time:
DateTime.fromMillisecondsSinceEpoch(json[_JsonFields.time] as int),
Expand Down Expand Up @@ -139,12 +142,11 @@ class LeakReport {
});

factory LeakReport.fromJson(Map<String, dynamic> json) => LeakReport(
type: json[_JsonFields.type],
context: (json[_JsonFields.context] as Map<String, dynamic>? ?? {})
.cast<String, dynamic>(),
code: json[_JsonFields.code],
trackedClass: json[_JsonFields.trackedClass] ?? '',
phase: json[_JsonFields.phase],
type: json[_JsonFields.type] as String,
context: json[_JsonFields.context] as Map<String, dynamic>? ?? {},
code: json[_JsonFields.code] as int,
trackedClass: json[_JsonFields.trackedClass] as String? ?? '',
phase: json[_JsonFields.phase] as String?,
);

/// Information about the leak that can help in troubleshooting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class _ListenedSink {

void checkStoreAndClear(List<LeakSummary> items) {
expect(store, hasLength(items.length));
for (final i in Iterable.generate(store.length)) {
for (final i in Iterable<int>.generate(store.length)) {
expect(store[i].toMessage(), contains(items[i].toMessage()));
}
store.clear();
Expand All @@ -217,7 +217,7 @@ class _MockStdoutSink implements StdoutSummarySink {

void checkStoreAndClear(List<LeakSummary> items) {
expect(store, hasLength(items.length));
for (final i in Iterable.generate(store.length)) {
for (final i in Iterable<int>.generate(store.length)) {
expect(store[i].toMessage(), contains(items[i].toMessage()));
}
store.clear();
Expand All @@ -232,7 +232,7 @@ class _MockDevToolsSink implements DevToolsSummarySink {

void checkStoreAndClear(List<LeakSummary> items) {
expect(store, hasLength(items.length));
for (final i in Iterable.generate(store.length)) {
for (final i in Iterable<int>.generate(store.length)) {
expect(store[i].toMessage(), contains(items[i].toMessage()));
}
store.clear();
Expand Down
2 changes: 1 addition & 1 deletion pkgs/leak_tracker_flutter_testing/lib/src/matchers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class _AreCreateAndDispose extends Matcher {
Map matchState,
bool verbose,
) {
return mismatchDescription..add(matchState[_key]);
return mismatchDescription..add(matchState[_key] as String);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void main() {
),
);

for (var i in Iterable.generate(10)) {
for (var i in Iterable<int>.generate(10)) {
testWidgetsWithLeakTracking(
'baselining with multiple runs',
(widgetTester) async {
Expand Down

0 comments on commit e85c0c0

Please sign in to comment.