Skip to content

Commit

Permalink
Remove unnecessary null checks in `flutter/{animation,semantics,sched…
Browse files Browse the repository at this point in the history
…uler}` (#118922)

* Remove unnecessary null checks in flutter/animation

* Remove unnecessary null checks in flutter/semantics

* Remove unnecessary null checks in flutter/scheduler
  • Loading branch information
goderbauer authored Jan 21, 2023
1 parent 5d74b5c commit 7272c80
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 120 deletions.
1 change: 0 additions & 1 deletion packages/flutter/lib/src/animation/animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ abstract class Animation<T> extends Listenable implements ValueListenable<T> {
/// * "&#x23ED;": [AnimationStatus.completed] ([value] == 1.0)
/// * "&#x23EE;": [AnimationStatus.dismissed] ([value] == 0.0)
String toStringDetails() {
assert(status != null);
switch (status) {
case AnimationStatus.forward:
return '\u25B6'; // >
Expand Down
16 changes: 3 additions & 13 deletions packages/flutter/lib/src/animation/animation_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,7 @@ class AnimationController extends Animation<double>
this.upperBound = 1.0,
this.animationBehavior = AnimationBehavior.normal,
required TickerProvider vsync,
}) : assert(lowerBound != null),
assert(upperBound != null),
assert(upperBound >= lowerBound),
assert(vsync != null),
}) : assert(upperBound >= lowerBound),
_direction = _AnimationDirection.forward {
_ticker = vsync.createTicker(_tick);
_internalSetValue(value ?? lowerBound);
Expand Down Expand Up @@ -275,9 +272,7 @@ class AnimationController extends Animation<double>
this.debugLabel,
required TickerProvider vsync,
this.animationBehavior = AnimationBehavior.preserve,
}) : assert(value != null),
assert(vsync != null),
lowerBound = double.negativeInfinity,
}) : lowerBound = double.negativeInfinity,
upperBound = double.infinity,
_direction = _AnimationDirection.forward {
_ticker = vsync.createTicker(_tick);
Expand Down Expand Up @@ -363,7 +358,6 @@ class AnimationController extends Animation<double>
/// * [forward], [reverse], [animateTo], [animateWith], [fling], and [repeat],
/// which start the animation controller.
set value(double newValue) {
assert(newValue != null);
stop();
_internalSetValue(newValue);
notifyListeners();
Expand Down Expand Up @@ -657,7 +651,6 @@ class AnimationController extends Animation<double>
}());
assert(max >= min);
assert(max <= upperBound && min >= lowerBound);
assert(reverse != null);
stop();
return _startSimulation(_RepeatingSimulation(_value, min, max, reverse, period!, _directionSetter));
}
Expand Down Expand Up @@ -744,7 +737,6 @@ class AnimationController extends Animation<double>
}

TickerFuture _startSimulation(Simulation simulation) {
assert(simulation != null);
assert(!isAnimating);
_simulation = simulation;
_lastElapsedDuration = Duration.zero;
Expand Down Expand Up @@ -856,9 +848,7 @@ class AnimationController extends Animation<double>

class _InterpolationSimulation extends Simulation {
_InterpolationSimulation(this._begin, this._end, Duration duration, this._curve, double scale)
: assert(_begin != null),
assert(_end != null),
assert(duration != null && duration.inMicroseconds > 0),
: assert(duration.inMicroseconds > 0),
_durationInSeconds = (duration.inMicroseconds * scale) / Duration.microsecondsPerSecond;

final double _durationInSeconds;
Expand Down
12 changes: 4 additions & 8 deletions packages/flutter/lib/src/animation/animations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ class ReverseAnimation extends Animation<double>
/// Creates a reverse animation.
///
/// The parent argument must not be null.
ReverseAnimation(this.parent)
: assert(parent != null);
ReverseAnimation(this.parent);

/// The animation whose value and direction this animation is reversing.
final Animation<double> parent;
Expand Down Expand Up @@ -310,7 +309,6 @@ class ReverseAnimation extends Animation<double>
double get value => 1.0 - parent.value;

AnimationStatus _reverseStatus(AnimationStatus status) {
assert(status != null);
switch (status) {
case AnimationStatus.forward: return AnimationStatus.reverse;
case AnimationStatus.reverse: return AnimationStatus.forward;
Expand Down Expand Up @@ -384,8 +382,7 @@ class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<do
required this.parent,
required this.curve,
this.reverseCurve,
}) : assert(parent != null),
assert(curve != null) {
}) {
_updateCurveDirection(parent.status);
parent.addStatusListener(_updateCurveDirection);
}
Expand Down Expand Up @@ -518,7 +515,7 @@ class TrainHoppingAnimation extends Animation<double>
Animation<double> this._currentTrain,
this._nextTrain, {
this.onSwitchedTrain,
}) : assert(_currentTrain != null) {
}) {
if (_nextTrain != null) {
if (_currentTrain!.value == _nextTrain!.value) {
_currentTrain = _nextTrain;
Expand Down Expand Up @@ -644,8 +641,7 @@ abstract class CompoundAnimation<T> extends Animation<T>
CompoundAnimation({
required this.first,
required this.next,
}) : assert(first != null),
assert(next != null);
});

/// The first sub-animation. Its status takes precedence if neither are
/// animating.
Expand Down
39 changes: 10 additions & 29 deletions packages/flutter/lib/src/animation/curves.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ abstract class ParametricCurve<T> {
/// implementation of [transform], which delegates the remaining logic to
/// [transformInternal].
T transform(double t) {
assert(t != null);
assert(t >= 0.0 && t <= 1.0, 'parametric value $t is outside of [0, 1] range.');
return transformInternal(t);
}
Expand Down Expand Up @@ -129,7 +128,7 @@ class SawTooth extends Curve {
/// Creates a sawtooth curve.
///
/// The [count] argument must not be null.
const SawTooth(this.count) : assert(count != null);
const SawTooth(this.count);

/// The number of repetitions of the sawtooth pattern in the unit interval.
final int count;
Expand Down Expand Up @@ -159,10 +158,7 @@ class Interval extends Curve {
/// Creates an interval curve.
///
/// The arguments must not be null.
const Interval(this.begin, this.end, { this.curve = Curves.linear })
: assert(begin != null),
assert(end != null),
assert(curve != null);
const Interval(this.begin, this.end, { this.curve = Curves.linear });

/// The largest value for which this interval is 0.0.
///
Expand Down Expand Up @@ -207,7 +203,7 @@ class Threshold extends Curve {
/// Creates a threshold curve.
///
/// The [threshold] argument must not be null.
const Threshold(this.threshold) : assert(threshold != null);
const Threshold(this.threshold);

/// The value before which the curve is 0.0 and after which the curve is 1.0.
///
Expand Down Expand Up @@ -307,11 +303,7 @@ class Cubic extends Curve {
/// cubic curves in [Curves].
///
/// The [a] (x1), [b] (y1), [c] (x2) and [d] (y2) arguments must not be null.
const Cubic(this.a, this.b, this.c, this.d)
: assert(a != null),
assert(b != null),
assert(c != null),
assert(d != null);
const Cubic(this.a, this.b, this.c, this.d);

/// The x coordinate of the first control point.
///
Expand Down Expand Up @@ -518,9 +510,6 @@ abstract class Curve2D extends ParametricCurve<Offset> {
// 4. Recursively sample the two parts.
//
// This algorithm concentrates samples in areas of high curvature.
assert(tolerance != null);
assert(start != null);
assert(end != null);
assert(end > start);
// We want to pick a random seed that will keep the result stable if
// evaluated again, so we use the first non-generated control point.
Expand Down Expand Up @@ -577,7 +566,6 @@ abstract class Curve2D extends ParametricCurve<Offset> {
/// single-valued, it will return the parameter for only one of the values at
/// the given `x` location.
double findInverse(double x) {
assert(x != null);
double start = 0.0;
double end = 1.0;
late double mid;
Expand Down Expand Up @@ -612,7 +600,7 @@ class Curve2DSample {
/// Creates an object that holds a sample; used with [Curve2D] subclasses.
///
/// All arguments must not be null.
const Curve2DSample(this.t, this.value) : assert(t != null), assert(value != null);
const Curve2DSample(this.t, this.value);

/// The parametric location of this sample point along the curve.
final double t;
Expand Down Expand Up @@ -682,9 +670,7 @@ class CatmullRomSpline extends Curve2D {
double tension = 0.0,
Offset? startHandle,
Offset? endHandle,
}) : assert(controlPoints != null),
assert(tension != null),
assert(tension <= 1.0, 'tension $tension must not be greater than 1.0.'),
}) : assert(tension <= 1.0, 'tension $tension must not be greater than 1.0.'),
assert(tension >= 0.0, 'tension $tension must not be negative.'),
assert(controlPoints.length > 3, 'There must be at least four control points to create a CatmullRomSpline.'),
_controlPoints = controlPoints,
Expand All @@ -702,9 +688,7 @@ class CatmullRomSpline extends Curve2D {
double tension = 0.0,
Offset? startHandle,
Offset? endHandle,
}) : assert(controlPoints != null),
assert(tension != null),
assert(tension <= 1.0, 'tension $tension must not be greater than 1.0.'),
}) : assert(tension <= 1.0, 'tension $tension must not be greater than 1.0.'),
assert(tension >= 0.0, 'tension $tension must not be negative.'),
assert(controlPoints.length > 3, 'There must be at least four control points to create a CatmullRomSpline.'),
_controlPoints = null,
Expand Down Expand Up @@ -860,8 +844,7 @@ class CatmullRomCurve extends Curve {
///
/// * This [paper on using Catmull-Rom splines](http://faculty.cs.tamu.edu/schaefer/research/cr_cad.pdf).
CatmullRomCurve(this.controlPoints, {this.tension = 0.0})
: assert(tension != null),
assert(() {
: assert(() {
return validateControlPoints(
controlPoints,
tension: tension,
Expand All @@ -877,8 +860,7 @@ class CatmullRomCurve extends Curve {
/// Same as [CatmullRomCurve.new], but it precomputes the internal curve data
/// structures for a more predictable computation load.
CatmullRomCurve.precompute(this.controlPoints, {this.tension = 0.0})
: assert(tension != null),
assert(() {
: assert(() {
return validateControlPoints(
controlPoints,
tension: tension,
Expand Down Expand Up @@ -963,7 +945,6 @@ class CatmullRomCurve extends Curve {
double tension = 0.0,
List<String>? reasons,
}) {
assert(tension != null);
if (controlPoints == null) {
assert(() {
reasons?.add('Supplied control points cannot be null');
Expand Down Expand Up @@ -1143,7 +1124,7 @@ class FlippedCurve extends Curve {
/// Creates a flipped curve.
///
/// The [curve] argument must not be null.
const FlippedCurve(this.curve) : assert(curve != null);
const FlippedCurve(this.curve);

/// The curve that is being flipped.
final Curve curve;
Expand Down
6 changes: 2 additions & 4 deletions packages/flutter/lib/src/animation/tween.dart
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,7 @@ class Tween<T extends Object?> extends Animatable<T> {
class ReverseTween<T extends Object?> extends Tween<T> {
/// Construct a [Tween] that evaluates its [parent] in reverse.
ReverseTween(this.parent)
: assert(parent != null),
super(begin: parent.end, end: parent.begin);
: super(begin: parent.end, end: parent.begin);

/// This tween's value is the same as the parent's value evaluated in reverse.
///
Expand Down Expand Up @@ -544,8 +543,7 @@ class CurveTween extends Animatable<double> {
/// Creates a curve tween.
///
/// The [curve] argument must not be null.
CurveTween({ required this.curve })
: assert(curve != null);
CurveTween({ required this.curve });

/// The curve to use when transforming the value of the animation.
Curve curve;
Expand Down
10 changes: 3 additions & 7 deletions packages/flutter/lib/src/animation/tween_sequence.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class TweenSequence<T> extends Animatable<T> {
/// best to reuse one, rather than rebuilding it on every frame, when that's
/// possible.
TweenSequence(List<TweenSequenceItem<T>> items)
: assert(items != null),
assert(items.isNotEmpty) {
: assert(items.isNotEmpty) {
_items.addAll(items);

double totalWeight = 0.0;
Expand Down Expand Up @@ -113,8 +112,7 @@ class FlippedTweenSequence extends TweenSequence<double> {
/// There's a small cost associated with building a `TweenSequence` so it's
/// best to reuse one, rather than rebuilding it on every frame, when that's
/// possible.
FlippedTweenSequence(super.items)
: assert(items != null);
FlippedTweenSequence(super.items);

@override
double transform(double t) => 1 - super.transform(1 - t);
Expand All @@ -128,9 +126,7 @@ class TweenSequenceItem<T> {
const TweenSequenceItem({
required this.tween,
required this.weight,
}) : assert(tween != null),
assert(weight != null),
assert(weight > 0.0);
}) : assert(weight > 0.0);

/// Defines the value of the [TweenSequence] for the interval within the
/// animation's duration indicated by [weight] and this item's position
Expand Down
3 changes: 0 additions & 3 deletions packages/flutter/lib/src/scheduler/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ mixin SchedulerBinding on BindingBase {
@protected
@mustCallSuper
void handleAppLifecycleStateChanged(AppLifecycleState state) {
assert(state != null);
_lifecycleState = state;
switch (state) {
case AppLifecycleState.resumed:
Expand Down Expand Up @@ -1026,7 +1025,6 @@ mixin SchedulerBinding on BindingBase {
/// presentation time, and can be used to ensure that animations running in
/// different processes are synchronized.
Duration get currentSystemFrameTimeStamp {
assert(_lastRawTimeStamp != null);
return _lastRawTimeStamp;
}

Expand Down Expand Up @@ -1279,7 +1277,6 @@ mixin SchedulerBinding on BindingBase {
// the error.
@pragma('vm:notify-debugger-on-exception')
void _invokeFrameCallback(FrameCallback callback, Duration timeStamp, [ StackTrace? callbackStack ]) {
assert(callback != null);
assert(_FrameCallbackEntry.debugCurrentCallbackStack == null);
assert(() {
_FrameCallbackEntry.debugCurrentCallbackStack = callbackStack;
Expand Down
Loading

0 comments on commit 7272c80

Please sign in to comment.