Skip to content

Commit b0a90ae

Browse files
authored
Enable strict-inference (flutter#135043)
Avoids that dynamic accidentally sneaks in, see https://dart.dev/tools/analysis#enabling-additional-type-checks
1 parent 96a4ae9 commit b0a90ae

File tree

27 files changed

+63
-55
lines changed

27 files changed

+63
-55
lines changed

Diff for: analysis_options.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
analyzer:
1818
language:
1919
strict-casts: true
20+
strict-inference: true
2021
strict-raw-types: true
2122
errors:
2223
# allow self-reference to deprecated members (we do this because otherwise we have

Diff for: dev/bots/test/analyze-snippet-code-test-dart-ui/ui.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ library dart.ui;
1111
///
1212
/// ```dart
1313
/// class MyStringBuffer {
14-
/// error; // error (missing_const_final_var_or_type, always_specify_types)
14+
/// error; // error (prefer_typing_uninitialized_variables, inference_failure_on_uninitialized_variable, missing_const_final_var_or_type)
1515
///
1616
/// StringBuffer _buffer = StringBuffer(); // error (prefer_final_fields, unused_field)
1717
/// }

Diff for: dev/bots/test/analyze-snippet-code-test-input/custom_imports_broken.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ String? bar;
1616

1717
/// error: widgets library was not imported (not even implicitly).
1818
/// ```dart
19-
/// print(Widget);
19+
/// print(Widget); // error (undefined_identifier)
2020
/// ```
2121
String? foo;

Diff for: dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
/// Widget build(BuildContext context) {
132132
/// final String title;
133133
/// return Opacity(
134-
/// key: globalKey, // error (undefined_identifier, argument_type_not_assignable)
134+
/// key: globalKey, // error (undefined_identifier)
135135
/// opacity: _visible ? 1.0 : 0.0,
136136
/// child: Text(title), // error (read_potentially_unassigned_final)
137137
/// );
@@ -144,13 +144,14 @@
144144
/// ```
145145
///
146146
/// ```dart
147-
/// import 'dart:io'; // error (unused_import)/// final Widget p = Placeholder(); // error (undefined_class, undefined_function)
147+
/// import 'dart:io'; // error (unused_import)
148+
/// final Widget p = Placeholder(); // error (undefined_class, undefined_function)
148149
/// ```
149150
///
150151
/// ```dart
151152
/// // (e.g. in a stateful widget)
152153
/// void initState() { // error (must_call_super, annotate_overrides)
153-
/// widget.toString(); // error (undefined_identifier, return_of_invalid_type)
154+
/// widget.toString();
154155
/// }
155156
/// ```
156157
///

Diff for: dev/bots/test/analyze_snippet_code_test.dart

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ const List<String> expectedMainErrors = <String>[
2121
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:134:14: (top-level declaration) (undefined_identifier)',
2222
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:136:21: (top-level declaration) (read_potentially_unassigned_final)',
2323
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:147:12: (self-contained program) (unused_import)',
24-
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:152:10: (stateful widget) (annotate_overrides)',
25-
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:152:10: (stateful widget) (must_call_super)',
26-
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:160:7: (top-level declaration) (undefined_identifier)',
27-
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:164: Found "```" in code but it did not match RegExp: pattern=^ */// *```dart\$ flags= so something is wrong. Line was: "/// ```"',
24+
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:148:11: (self-contained program) (undefined_class)',
25+
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:148:22: (self-contained program) (undefined_function)',
26+
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:153:10: (stateful widget) (annotate_overrides)',
27+
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:153:10: (stateful widget) (must_call_super)',
28+
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:161:7: (top-level declaration) (undefined_identifier)',
29+
'dev/bots/test/analyze-snippet-code-test-input/known_broken_documentation.dart:165: Found "```" in code but it did not match RegExp: pattern=^ */// *```dart\$ flags= so something is wrong. Line was: "/// ```"',
2830
'dev/bots/test/analyze-snippet-code-test-input/short_but_still_broken.dart:9:12: (statement) (invalid_assignment)',
2931
'dev/bots/test/analyze-snippet-code-test-input/short_but_still_broken.dart:18:4: Empty ```dart block in snippet code.',
3032
];
3133

3234
const List<String> expectedUiErrors = <String>[
3335
'dev/bots/test/analyze-snippet-code-test-dart-ui/ui.dart:14:7: (top-level declaration) (prefer_typing_uninitialized_variables)',
36+
'dev/bots/test/analyze-snippet-code-test-dart-ui/ui.dart:14:7: (top-level declaration) (inference_failure_on_uninitialized_variable)',
3437
'dev/bots/test/analyze-snippet-code-test-dart-ui/ui.dart:14:7: (top-level declaration) (missing_const_final_var_or_type)',
3538
'dev/bots/test/analyze-snippet-code-test-dart-ui/ui.dart:16:20: (top-level declaration) (prefer_final_fields)',
3639
'dev/bots/test/analyze-snippet-code-test-dart-ui/ui.dart:16:20: (top-level declaration) (unused_field)',
@@ -69,7 +72,7 @@ void main() {
6972
final List<String> stderrNoDescriptions = stderrLines.map(removeLintDescriptions).toList();
7073
expect(stderrNoDescriptions, <String>[
7174
...expectedMainErrors,
72-
'Found 16 snippet code errors.',
75+
'Found 18 snippet code errors.',
7376
'See the documentation at the top of dev/bots/analyze_snippet_code.dart for details.',
7477
'', // because we end with a newline, split gives us an extra blank line
7578
]);
@@ -93,7 +96,7 @@ void main() {
9396
expect(stderrNoDescriptions, <String>[
9497
...expectedUiErrors,
9598
...expectedMainErrors,
96-
'Found 20 snippet code errors.',
99+
'Found 23 snippet code errors.',
97100
'See the documentation at the top of dev/bots/analyze_snippet_code.dart for details.',
98101
'', // because we end with a newline, split gives us an extra blank line
99102
]);

Diff for: dev/conductor/core/test/packages_autoroller_test.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void main() {
170170
await expectLater(
171171
() async {
172172
final Future<void> rollFuture = autoroller.roll();
173-
await controller.stream.drain();
173+
await controller.stream.drain<Object?>();
174174
await rollFuture;
175175
},
176176
throwsA(isA<Exception>().having(
@@ -214,7 +214,7 @@ void main() {
214214
], stdout: '[{"number": 123}]'),
215215
]);
216216
final Future<void> rollFuture = autoroller.roll();
217-
await controller.stream.drain();
217+
await controller.stream.drain<Object?>();
218218
await rollFuture;
219219
expect(processManager, hasNoRemainingExpectations);
220220
expect(stdio.stdout, contains('flutter-pub-roller-bot already has open tool PRs'));
@@ -312,7 +312,7 @@ void main() {
312312
]),
313313
]);
314314
final Future<void> rollFuture = autoroller.roll();
315-
await controller.stream.drain();
315+
await controller.stream.drain<Object?>();
316316
await rollFuture;
317317
expect(processManager, hasNoRemainingExpectations);
318318
});

Diff for: dev/devicelab/bin/tasks/flutter_engine_group_performance.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const String _activityName = 'MainActivity';
1616
const int _numberOfIterations = 10;
1717

1818
Future<void> _withApkInstall(
19-
String apkPath, String bundleName, Function(AndroidDevice) body) async {
19+
String apkPath, String bundleName, Future<void> Function(AndroidDevice) body) async {
2020
final DeviceDiscovery devices = DeviceDiscovery();
2121
final AndroidDevice device = await devices.workingDevice as AndroidDevice;
2222
await device.unlock();

Diff for: dev/devicelab/lib/framework/runner.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Future<void> runTasks(
4242
List<String>? taskArgs,
4343
bool useEmulator = false,
4444
@visibleForTesting Map<String, String>? isolateParams,
45-
@visibleForTesting Function(String) print = print,
45+
@visibleForTesting void Function(String) print = print,
4646
@visibleForTesting List<String>? logs,
4747
}) async {
4848
for (final String taskName in taskNames) {

Diff for: dev/integration_tests/ios_platform_view_tests/lib/main.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class _ZOrderTestPageState extends State<ZOrderTestPage> {
201201
)),
202202
TextButton(
203203
onPressed: () {
204-
showDialog(
204+
showDialog<void>(
205205
context: context,
206206
builder: (BuildContext context) {
207207
return const SizedBox(

Diff for: examples/api/lib/material/bottom_navigation_bar/bottom_navigation_bar.2.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class _BottomNavigationBarExampleState extends State<BottomNavigationBarExample>
9191
}
9292

9393
void showModal(BuildContext context) {
94-
showDialog(
94+
showDialog<void>(
9595
context: context,
9696
builder: (BuildContext context) => AlertDialog(
9797
content: const Text('Example Dialog'),

Diff for: examples/api/lib/material/navigation_bar/navigation_bar.2.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class RootPage extends StatelessWidget {
174174
ElevatedButton(
175175
style: buttonStyle,
176176
onPressed: () {
177-
showDialog(
177+
showDialog<void>(
178178
context: context,
179179
useRootNavigator: false,
180180
builder: _buildDialog,
@@ -186,9 +186,9 @@ class RootPage extends StatelessWidget {
186186
ElevatedButton(
187187
style: buttonStyle,
188188
onPressed: () {
189-
showDialog(
189+
showDialog<void>(
190190
context: context,
191-
useRootNavigator: true,
191+
useRootNavigator: true, // ignore: avoid_redundant_argument_values
192192
builder: _buildDialog,
193193
);
194194
},
@@ -200,7 +200,7 @@ class RootPage extends StatelessWidget {
200200
return ElevatedButton(
201201
style: buttonStyle,
202202
onPressed: () {
203-
showBottomSheet(
203+
showBottomSheet<void>(
204204
context: context,
205205
builder: (BuildContext context) {
206206
return Container(

Diff for: examples/api/lib/widgets/scroll_view/list_view.0.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class GridBuilder extends StatefulWidget {
136136
});
137137

138138
final bool isSelectionMode;
139-
final Function(bool)? onSelectionChange;
139+
final ValueChanged<bool>? onSelectionChange;
140140
final List<bool> selectedList;
141141

142142
@override
@@ -189,7 +189,7 @@ class ListBuilder extends StatefulWidget {
189189

190190
final bool isSelectionMode;
191191
final List<bool> selectedList;
192-
final Function(bool)? onSelectionChange;
192+
final ValueChanged<bool>? onSelectionChange;
193193

194194
@override
195195
State<ListBuilder> createState() => _ListBuilderState();

Diff for: packages/flutter/lib/src/cupertino/toggleable.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ mixin ToggleableStateMixin<S extends StatefulWidget> on TickerProviderStateMixin
111111
/// build method - potentially after wrapping it in other widgets.
112112
Widget buildToggleable({
113113
FocusNode? focusNode,
114-
Function(bool)? onFocusChange,
114+
ValueChanged<bool>? onFocusChange,
115115
bool autofocus = false,
116116
required Size size,
117117
required CustomPainter painter,

Diff for: packages/flutter/lib/src/material/bottom_sheet.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class _DragHandle extends StatelessWidget {
440440
});
441441

442442
final VoidCallback? onSemanticsTap;
443-
final Function(bool) handleHover;
443+
final ValueChanged<bool> handleHover;
444444
final Set<MaterialState> materialState;
445445
final Color? dragHandleColor;
446446
final Size? dragHandleSize;

Diff for: packages/flutter/lib/src/material/switch.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ class _MaterialSwitch extends StatefulWidget {
710710
final MaterialStateProperty<Color?>? overlayColor;
711711
final double? splashRadius;
712712
final FocusNode? focusNode;
713-
final Function(bool)? onFocusChange;
713+
final ValueChanged<bool>? onFocusChange;
714714
final bool autofocus;
715715
final Size size;
716716

Diff for: packages/flutter/lib/src/material/toggleable.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ mixin ToggleableStateMixin<S extends StatefulWidget> on TickerProviderStateMixin
302302
/// build method - potentially after wrapping it in other widgets.
303303
Widget buildToggleable({
304304
FocusNode? focusNode,
305-
Function(bool)? onFocusChange,
305+
ValueChanged<bool>? onFocusChange,
306306
bool autofocus = false,
307307
required MaterialStateProperty<MouseCursor> mouseCursor,
308308
required Size size,

Diff for: packages/flutter/lib/src/widgets/navigator.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -5470,7 +5470,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
54705470
}
54715471

54725472
/// Gets first route entry satisfying the predicate, or null if not found.
5473-
_RouteEntry? _firstRouteEntryWhereOrNull<T>(_RouteEntryPredicate test) {
5473+
_RouteEntry? _firstRouteEntryWhereOrNull(_RouteEntryPredicate test) {
54745474
for (final _RouteEntry element in _history) {
54755475
if (test(element)) {
54765476
return element;
@@ -5480,7 +5480,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
54805480
}
54815481

54825482
/// Gets last route entry satisfying the predicate, or null if not found.
5483-
_RouteEntry? _lastRouteEntryWhereOrNull<T>(_RouteEntryPredicate test) {
5483+
_RouteEntry? _lastRouteEntryWhereOrNull(_RouteEntryPredicate test) {
54845484
_RouteEntry? result;
54855485
for (final _RouteEntry element in _history) {
54865486
if (test(element)) {

Diff for: packages/flutter/test/material/popup_menu_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -3352,7 +3352,7 @@ void main() {
33523352
itemBuilder: (BuildContext context) => <PopupMenuItem<int>>[
33533353
PopupMenuItem<int>(
33543354
onTap: () {
3355-
showModalBottomSheet(
3355+
showModalBottomSheet<void>(
33563356
context: context,
33573357
builder: (BuildContext context) {
33583358
return const SizedBox(

Diff for: packages/flutter/test/painting/colors_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ void main() {
464464
});
465465

466466
test('ColorSwatch.lerp identical a,b', () {
467-
expect(ColorSwatch.lerp(null, null, 0), null);
467+
expect(ColorSwatch.lerp<Object?>(null, null, 0), null);
468468
const ColorSwatch<int> color = ColorSwatch<int>(0x00000000, <int, Color>{1: Color(0x00000000)});
469469
expect(identical(ColorSwatch.lerp(color, color, 0.5), color), true);
470470
});

Diff for: packages/flutter/test/rendering/proxy_box_test.dart

+7-5
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ void main() {
881881
});
882882

883883
// Simulate painting a RenderBox as if 'debugPaintSizeEnabled == true'
884-
Function(PaintingContext, Offset) debugPaint(RenderBox renderBox) {
884+
DebugPaintCallback debugPaint(RenderBox renderBox) {
885885
layout(renderBox);
886886
pumpFrame(phase: EnginePhase.compositingBits);
887887
return (PaintingContext context, Offset offset) {
@@ -891,7 +891,7 @@ void main() {
891891
}
892892

893893
test('RenderClipPath.debugPaintSize draws a path and a debug text when clipBehavior is not Clip.none', () {
894-
Function(PaintingContext, Offset) debugPaintClipRect(Clip clip) {
894+
DebugPaintCallback debugPaintClipRect(Clip clip) {
895895
final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
896896
final RenderClipPath renderClipPath = RenderClipPath(clipBehavior: clip, child: child);
897897
return debugPaint(renderClipPath);
@@ -908,7 +908,7 @@ void main() {
908908
});
909909

910910
test('RenderClipRect.debugPaintSize draws a rect and a debug text when clipBehavior is not Clip.none', () {
911-
Function(PaintingContext, Offset) debugPaintClipRect(Clip clip) {
911+
DebugPaintCallback debugPaintClipRect(Clip clip) {
912912
final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
913913
final RenderClipRect renderClipRect = RenderClipRect(clipBehavior: clip, child: child);
914914
return debugPaint(renderClipRect);
@@ -924,7 +924,7 @@ void main() {
924924
});
925925

926926
test('RenderClipRRect.debugPaintSize draws a rounded rect and a debug text when clipBehavior is not Clip.none', () {
927-
Function(PaintingContext, Offset) debugPaintClipRRect(Clip clip) {
927+
DebugPaintCallback debugPaintClipRRect(Clip clip) {
928928
final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
929929
final RenderClipRRect renderClipRRect = RenderClipRRect(clipBehavior: clip, child: child);
930930
return debugPaint(renderClipRRect);
@@ -940,7 +940,7 @@ void main() {
940940
});
941941

942942
test('RenderClipOval.debugPaintSize draws a path and a debug text when clipBehavior is not Clip.none', () {
943-
Function(PaintingContext, Offset) debugPaintClipOval(Clip clip) {
943+
DebugPaintCallback debugPaintClipOval(Clip clip) {
944944
final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
945945
final RenderClipOval renderClipOval = RenderClipOval(clipBehavior: clip, child: child);
946946
return debugPaint(renderClipOval);
@@ -1085,3 +1085,5 @@ void expectAssertionError() {
10851085
FlutterError.reportError(errorDetails);
10861086
}
10871087
}
1088+
1089+
typedef DebugPaintCallback = void Function(PaintingContext context, Offset offset);

Diff for: packages/flutter/test/widgets/media_query_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1111

1212
class _MediaQueryAspectCase {
1313
const _MediaQueryAspectCase(this.method, this.data);
14-
final Function(BuildContext) method;
14+
final void Function(BuildContext) method;
1515
final MediaQueryData data;
1616
}
1717

Diff for: packages/flutter/test/widgets/sliver_cross_axis_group_test.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter/rendering.dart';
78
import 'package:flutter_test/flutter_test.dart';
@@ -327,7 +328,7 @@ void main() {
327328

328329
testWidgets('Assertion error when SliverExpanded is used outside of SliverCrossAxisGroup', (WidgetTester tester) async {
329330
final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
330-
final Function(FlutterErrorDetails)? oldHandler = FlutterError.onError;
331+
final FlutterExceptionHandler? oldHandler = FlutterError.onError;
331332
FlutterError.onError = (FlutterErrorDetails error) => errors.add(error);
332333

333334
await tester.pumpWidget(
@@ -459,7 +460,7 @@ void main() {
459460

460461
testWidgetsWithLeakTracking('Assertion error when constrained widget runs out of cross axis extent', (WidgetTester tester) async {
461462
final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
462-
final Function(FlutterErrorDetails)? oldHandler = FlutterError.onError;
463+
final FlutterExceptionHandler? oldHandler = FlutterError.onError;
463464
FlutterError.onError = (FlutterErrorDetails error) => errors.add(error);
464465

465466
final List<int> items = List<int>.generate(20, (int i) => i);
@@ -481,7 +482,7 @@ void main() {
481482

482483
testWidgetsWithLeakTracking('Assertion error when expanded widget runs out of cross axis extent', (WidgetTester tester) async {
483484
final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
484-
final Function(FlutterErrorDetails)? oldHandler = FlutterError.onError;
485+
final FlutterExceptionHandler? oldHandler = FlutterError.onError;
485486
FlutterError.onError = (FlutterErrorDetails error) => errors.add(error);
486487

487488
final List<int> items = List<int>.generate(20, (int i) => i);

Diff for: packages/flutter_test/lib/src/_binding_io.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class _MockHttpClient implements HttpClient {
128128
bool Function(X509Certificate cert, String host, int port)? badCertificateCallback;
129129

130130
@override
131-
Function(String line)? keyLog;
131+
void Function(String line)? keyLog;
132132

133133
@override
134134
void close({ bool force = false }) { }

Diff for: packages/flutter_test/test/utils/fake_and_mock_utils.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void verifyPropertyFaked<TProperty>({
1919
required TProperty realValue,
2020
required TProperty fakeValue,
2121
required TProperty Function() propertyRetriever,
22-
required Function(TestWidgetsFlutterBinding, TProperty fakeValue) propertyFaker,
22+
required void Function(TestWidgetsFlutterBinding, TProperty fakeValue) propertyFaker,
2323
Matcher Function(TProperty) matcher = equals,
2424
}) {
2525
TProperty propertyBeforeFaking;
@@ -45,8 +45,8 @@ void verifyPropertyReset<TProperty>({
4545
required WidgetTester tester,
4646
required TProperty fakeValue,
4747
required TProperty Function() propertyRetriever,
48-
required Function() propertyResetter,
49-
required Function(TProperty fakeValue) propertyFaker,
48+
required VoidCallback propertyResetter,
49+
required ValueSetter<TProperty> propertyFaker,
5050
Matcher Function(TProperty) matcher = equals,
5151
}) {
5252
TProperty propertyBeforeFaking;

0 commit comments

Comments
 (0)