From b554f893bd2ceff77592a022c01f0bc4d3774e57 Mon Sep 17 00:00:00 2001 From: Abhishek Ghaskata Date: Fri, 14 May 2021 23:14:03 +0530 Subject: [PATCH] Enable unnecessary_null_checks lint (#82084) --- analysis_options.yaml | 2 +- dev/snippets/lib/snippets.dart | 4 +++- packages/flutter/lib/src/cupertino/form_section.dart | 4 ++-- packages/flutter/lib/src/material/date_picker.dart | 2 +- packages/flutter/lib/src/material/dialog.dart | 2 +- .../flutter/lib/src/material/reorderable_list.dart | 2 +- .../test/foundation/change_notifier_test.dart | 4 ++-- packages/flutter/test/services/restoration_test.dart | 2 +- .../flutter/test/widgets/editable_text_test.dart | 12 ++++++------ packages/flutter/test/widgets/focus_scope_test.dart | 2 +- .../widgets/scroll_aware_image_provider_test.dart | 4 ++-- packages/flutter/test/widgets/shortcuts_test.dart | 2 +- packages/flutter_test/lib/src/test_async_utils.dart | 3 ++- packages/flutter_tools/lib/src/dart/analysis.dart | 2 +- .../lib/src/fuchsia_remote_connection.dart | 2 +- 15 files changed, 26 insertions(+), 23 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index eb021288d06..4730a98e781 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -204,7 +204,7 @@ linter: # - unnecessary_lambdas # has false positives: https://github.com/dart-lang/linter/issues/498 - unnecessary_new - unnecessary_null_aware_assignments - # - unnecessary_null_checks # not yet tested + - unnecessary_null_checks - unnecessary_null_in_if_null_operators - unnecessary_nullable_for_final_variable_declarations - unnecessary_overrides diff --git a/dev/snippets/lib/snippets.dart b/dev/snippets/lib/snippets.dart index 029da5baf77..2dddca0b810 100644 --- a/dev/snippets/lib/snippets.dart +++ b/dev/snippets/lib/snippets.dart @@ -227,7 +227,9 @@ class SnippetGenerator { if (match != null) { // If we saw the start or end of a code block inCodeBlock = !inCodeBlock; if (match.namedGroup('language') != null) { - language = match[1]!; + language = match[1]; + assert(language != null); + language = language!; if (match.namedGroup('section') != null) { components.add(_ComponentTuple('code-${match.namedGroup('section')}', [], language: language)); } else { diff --git a/packages/flutter/lib/src/cupertino/form_section.dart b/packages/flutter/lib/src/cupertino/form_section.dart index b164d088501..44e9246c4f5 100644 --- a/packages/flutter/lib/src/cupertino/form_section.dart +++ b/packages/flutter/lib/src/cupertino/form_section.dart @@ -287,7 +287,7 @@ class CupertinoFormSection extends StatelessWidget { ), child: Padding( padding: _kDefaultHeaderMargin, - child: header!, + child: header, ), ), ), @@ -311,7 +311,7 @@ class CupertinoFormSection extends StatelessWidget { ), child: Padding( padding: _kDefaultFooterMargin, - child: footer!, + child: footer, ), ), ), diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index b20b9e52e3b..a7a9ed00b39 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -1652,7 +1652,7 @@ class _CalendarRangePickerDialog extends StatelessWidget { if (orientation == Orientation.portrait && entryModeButton != null) Padding( padding: const EdgeInsets.symmetric(horizontal: 8.0), - child: entryModeButton!, + child: entryModeButton, ), ]), ), diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index d915ecac241..340e4e9c8e0 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -528,7 +528,7 @@ class AlertDialog extends StatelessWidget { style: contentTextStyle ?? dialogTheme.contentTextStyle ?? theme.textTheme.subtitle1!, child: Semantics( container: true, - child: content!, + child: content, ), ), ); diff --git a/packages/flutter/lib/src/material/reorderable_list.dart b/packages/flutter/lib/src/material/reorderable_list.dart index 74e21eb3640..6ec672a30f4 100644 --- a/packages/flutter/lib/src/material/reorderable_list.dart +++ b/packages/flutter/lib/src/material/reorderable_list.dart @@ -557,7 +557,7 @@ class _ReorderableListViewState extends State { if (widget.header != null) SliverPadding( padding: headerPadding, - sliver: SliverToBoxAdapter(child: widget.header!), + sliver: SliverToBoxAdapter(child: widget.header), ), SliverPadding( padding: listPadding, diff --git a/packages/flutter/test/foundation/change_notifier_test.dart b/packages/flutter/test/foundation/change_notifier_test.dart index bca63e324ee..5cb15091e61 100644 --- a/packages/flutter/test/foundation/change_notifier_test.dart +++ b/packages/flutter/test/foundation/change_notifier_test.dart @@ -454,9 +454,9 @@ void main() { error = e; } expect(error, isNotNull); - expect(error!, isFlutterError); + expect(error, isFlutterError); expect( - error.toStringDeep(), + error!.toStringDeep(), equalsIgnoringHashCodes( 'FlutterError\n' ' A TestNotifier was used after being disposed.\n' diff --git a/packages/flutter/test/services/restoration_test.dart b/packages/flutter/test/services/restoration_test.dart index 7a92d4de47c..cc132308c3a 100644 --- a/packages/flutter/test/services/restoration_test.dart +++ b/packages/flutter/test/services/restoration_test.dart @@ -257,7 +257,7 @@ void main() { rootBucket2 = bucket; }); expect(rootBucket2, isNotNull); - expect(rootBucket2!, isNot(same(rootBucket))); + expect(rootBucket2, isNot(same(rootBucket))); expect(manager.isReplacing, isTrue); expect(rootBucket2!.isReplacing, isTrue); await tester.idle(); diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 591fffd277d..03da48bce8d 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -4861,7 +4861,7 @@ void main() { ); expect( - selection!, + selection, equals( const TextSelection( baseOffset: 0, @@ -4884,7 +4884,7 @@ void main() { ); expect( - selection!, + selection, equals( const TextSelection( baseOffset: 0, @@ -4912,7 +4912,7 @@ void main() { ); expect( - selection!, + selection, equals( const TextSelection( baseOffset: 0, @@ -4938,7 +4938,7 @@ void main() { platform: platform, ); expect( - selection!, + selection, equals( const TextSelection( baseOffset: 0, @@ -4959,7 +4959,7 @@ void main() { platform: platform, ); expect( - selection!, + selection, equals( const TextSelection( baseOffset: 0, @@ -7715,7 +7715,7 @@ class NoImplicitScrollPhysics extends AlwaysScrollableScrollPhysics { @override NoImplicitScrollPhysics applyTo(ScrollPhysics? ancestor) { - return NoImplicitScrollPhysics(parent: buildParent(ancestor)!); + return NoImplicitScrollPhysics(parent: buildParent(ancestor)); } } diff --git a/packages/flutter/test/widgets/focus_scope_test.dart b/packages/flutter/test/widgets/focus_scope_test.dart index 407ae250f9f..b1d583b69ca 100644 --- a/packages/flutter/test/widgets/focus_scope_test.dart +++ b/packages/flutter/test/widgets/focus_scope_test.dart @@ -263,7 +263,7 @@ void main() { ); // Add the child focus scope to the focus tree. - final FocusAttachment childAttachment = childFocusScope.attach(key.currentContext!); + final FocusAttachment childAttachment = childFocusScope.attach(key.currentContext); parentFocusScope.setFirstFocus(childFocusScope); await tester.pumpAndSettle(); expect(childFocusScope.isFirstFocus, isTrue); diff --git a/packages/flutter/test/widgets/scroll_aware_image_provider_test.dart b/packages/flutter/test/widgets/scroll_aware_image_provider_test.dart index 885a28bcb37..954d1e84b41 100644 --- a/packages/flutter/test/widgets/scroll_aware_image_provider_test.dart +++ b/packages/flutter/test/widgets/scroll_aware_image_provider_test.dart @@ -417,7 +417,7 @@ class RecordingPhysics extends ScrollPhysics { @override RecordingPhysics applyTo(ScrollPhysics? ancestor) { - return RecordingPhysics(parent: buildParent(ancestor)!); + return RecordingPhysics(parent: buildParent(ancestor)); } @override @@ -437,7 +437,7 @@ class ControllablePhysics extends ScrollPhysics { @override ControllablePhysics applyTo(ScrollPhysics? ancestor) { - return ControllablePhysics(parent: buildParent(ancestor)!); + return ControllablePhysics(parent: buildParent(ancestor)); } @override diff --git a/packages/flutter/test/widgets/shortcuts_test.dart b/packages/flutter/test/widgets/shortcuts_test.dart index ac6088d4cd2..bf8204bca16 100644 --- a/packages/flutter/test/widgets/shortcuts_test.dart +++ b/packages/flutter/test/widgets/shortcuts_test.dart @@ -26,7 +26,7 @@ class TestDispatcher extends ActionDispatcher { @override Object? invokeAction(Action action, Intent intent, [BuildContext? context]) { final Object? result = super.invokeAction(action, intent, context); - postInvoke?.call(action: action, intent: intent, context: context!, dispatcher: this); + postInvoke?.call(action: action, intent: intent, context: context, dispatcher: this); return result; } } diff --git a/packages/flutter_test/lib/src/test_async_utils.dart b/packages/flutter_test/lib/src/test_async_utils.dart index 3b98b7eab5a..46a5c9f211c 100644 --- a/packages/flutter_test/lib/src/test_async_utils.dart +++ b/packages/flutter_test/lib/src/test_async_utils.dart @@ -307,8 +307,9 @@ class TestAsyncUtils { do { // skip past frames that are from this class index += 1; assert(index < stack.length); - lineMatch = getClassPattern.matchAsPrefix(stack[index])!; + lineMatch = getClassPattern.matchAsPrefix(stack[index]); assert(lineMatch != null); + lineMatch = lineMatch!; assert(lineMatch.groupCount == 1); } while (lineMatch.group(1) == _className); // try to parse the stack to find the interesting frame diff --git a/packages/flutter_tools/lib/src/dart/analysis.dart b/packages/flutter_tools/lib/src/dart/analysis.dart index dd717fd6327..43dd3ee9cce 100644 --- a/packages/flutter_tools/lib/src/dart/analysis.dart +++ b/packages/flutter_tools/lib/src/dart/analysis.dart @@ -137,7 +137,7 @@ class AnalysisServer { } } else if (response['error'] != null) { // Fields are 'code', 'message', and 'stackTrace'. - final Map error = castStringKeyedMap(response['error']!)!; + final Map error = castStringKeyedMap(response['error'])!; _logger.printError( 'Error response from the server: ${error['code']} ${error['message']}'); if (error['stackTrace'] != null) { diff --git a/packages/fuchsia_remote_debug_protocol/lib/src/fuchsia_remote_connection.dart b/packages/fuchsia_remote_debug_protocol/lib/src/fuchsia_remote_connection.dart index e73068d46a2..409c2d9d5fb 100644 --- a/packages/fuchsia_remote_debug_protocol/lib/src/fuchsia_remote_connection.dart +++ b/packages/fuchsia_remote_debug_protocol/lib/src/fuchsia_remote_connection.dart @@ -210,7 +210,7 @@ class FuchsiaRemoteConnection { SshCommandRunner( address: address, interface: interface, - sshConfigPath: sshConfigPath!, + sshConfigPath: sshConfigPath, ), ); }