919461badd
Fixes https://github.com/dart-lang/sdk/issues/34147. The Dart language specification says we should ensure booleans are non-null in the following (non-exhaustive) situations: * (ternary) conditional expressions - this includes the `!` operator since `!x` is equivalent to `x ? false : true` * arguments to logical boolean expressions (`||` and `&&`), modulo short-circuiting * `if` conditions * `for` loop conditions * `while` conditions * `do`/`while` conditions With control-flow-collections enabled, this CL will also cover conditions in `if` elements and `for` elements. Tests for these already exist in language_2/control_flow_collections/{if,for}_null_condition_test.dart. Change-Id: I2ce9a30adeb16a0a68411f358f69aeca08656dab Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/98780 Commit-Queue: Mayank Patke <fishythefish@google.com> Reviewed-by: Stephen Adams <sra@google.com> Reviewed-by: Sigmund Cherem <sigmund@google.com>
73 lines
1.4 KiB
Dart
73 lines
1.4 KiB
Dart
// Copyright (c) 2019, 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.
|
|
|
|
import 'package:expect/expect.dart';
|
|
|
|
void main() {
|
|
conditionalTest();
|
|
orTest();
|
|
andTest();
|
|
ifTest();
|
|
forTest();
|
|
whileTest();
|
|
doTest();
|
|
notTest();
|
|
}
|
|
|
|
void conditionalTest() {
|
|
bool x = null;
|
|
Expect.throwsAssertionError(() => x ? 1 : 0);
|
|
}
|
|
|
|
void orTest() {
|
|
bool x = null;
|
|
Expect.throwsAssertionError(() => x || x);
|
|
Expect.throwsAssertionError(() => x || false);
|
|
Expect.throwsAssertionError(() => x || true);
|
|
Expect.throwsAssertionError(() => false || x);
|
|
Expect.isTrue(true || x);
|
|
}
|
|
|
|
void andTest() {
|
|
bool x = null;
|
|
Expect.throwsAssertionError(() => x && x);
|
|
Expect.throwsAssertionError(() => x && false);
|
|
Expect.throwsAssertionError(() => x && true);
|
|
Expect.isFalse(false && x);
|
|
Expect.throwsAssertionError(() => true && x);
|
|
}
|
|
|
|
void ifTest() {
|
|
bool x = null;
|
|
Expect.throwsAssertionError(() {
|
|
if (x) {}
|
|
});
|
|
}
|
|
|
|
void forTest() {
|
|
bool x = null;
|
|
Expect.throwsAssertionError(() {
|
|
for (; x;) {}
|
|
});
|
|
}
|
|
|
|
void whileTest() {
|
|
bool x = null;
|
|
Expect.throwsAssertionError(() {
|
|
while (x) {}
|
|
});
|
|
}
|
|
|
|
void doTest() {
|
|
bool x = null;
|
|
Expect.throwsAssertionError(() {
|
|
do {} while (x);
|
|
});
|
|
}
|
|
|
|
void notTest() {
|
|
bool x = null;
|
|
Expect.throwsAssertionError(() => !x);
|
|
}
|