From d80aa480011d4779295b23b0a8a880b2215f1dbe Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Fri, 14 Aug 2020 14:18:38 -0700 Subject: [PATCH] Ignore/fix dead_code checks for weak-only null checks. (#63794) Now that https://github.com/dart-lang/sdk/issues/41985 is fixed, the analyzer detects dead code due to "unnecessary" null checks. Since we want to retain null checks in Flutter even when they appear unnecessary (in order to preserve runtime behavior in weak mode), we need to suppress these dead code hints. Note that one assertion is removed by this PR (`heightFactor != null || (heightFactor == 1.0 && heightDelta == 0.0)`). This is because `heightFactor == 1.0 && heightDelta == 0.0` can only be true if `heightFactor != null`, so this assertion is equivalent to simply asserting that `heightFactor != null`, which is already asserted two lines above. --- packages/flutter/lib/src/painting/text_span.dart | 10 ++++++++-- packages/flutter/lib/src/painting/text_style.dart | 1 - 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/painting/text_span.dart b/packages/flutter/lib/src/painting/text_span.dart index 497abb7297..c057dfc1e9 100644 --- a/packages/flutter/lib/src/painting/text_span.dart +++ b/packages/flutter/lib/src/painting/text_span.dart @@ -366,7 +366,10 @@ class TextSpan extends InlineSpan { assert(() { if (children != null) { for (final InlineSpan child in children!) { - if (child == null) { + // `child` has a non-nullable return type, but might be null when + // running with weak checking, so we need to null check it anyway (and + // ignore the warning that the null-handling logic is dead code). + if (child == null) { // ignore: dead_code throw FlutterError.fromParts([ ErrorSummary('TextSpan contains a null child.'), ErrorDescription( @@ -474,9 +477,12 @@ class TextSpan extends InlineSpan { if (children == null) return const []; return children!.map((InlineSpan child) { + // `child` has a non-nullable return type, but might be null when running + // with weak checking, so we need to null check it anyway (and ignore the + // warning that the null-handling logic is dead code). if (child != null) { return child.toDiagnosticsNode(); - } else { + } else { // ignore: dead_code return DiagnosticsNode.message(''); } }).toList(); diff --git a/packages/flutter/lib/src/painting/text_style.dart b/packages/flutter/lib/src/painting/text_style.dart index b452f99333..e62cfc90b1 100644 --- a/packages/flutter/lib/src/painting/text_style.dart +++ b/packages/flutter/lib/src/painting/text_style.dart @@ -821,7 +821,6 @@ class TextStyle with Diagnosticable { assert(wordSpacing != null || (wordSpacingFactor == 1.0 && wordSpacingDelta == 0.0)); assert(heightFactor != null); assert(heightDelta != null); - assert(heightFactor != null || (heightFactor == 1.0 && heightDelta == 0.0)); assert(decorationThicknessFactor != null); assert(decorationThicknessDelta != null); assert(decorationThickness != null || (decorationThicknessFactor == 1.0 && decorationThicknessDelta == 0.0));