|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +void main() { |
| 6 | + Never never = throw "Unreachable"; |
| 7 | + bool boolean = true; |
| 8 | + dynamic any = 3; |
| 9 | + Null nil = null; |
| 10 | + Object object = Object(); |
| 11 | + Object? objectOrNull = null; |
| 12 | + |
| 13 | + { |
| 14 | + // Check that values of type `Never` are usable as booleans. |
| 15 | + if (never) {} |
| 16 | + [if (never) 3]; |
| 17 | + never ? 3 : 4; |
| 18 | + while (never) {} |
| 19 | + do {} while (never); |
| 20 | + never || true; |
| 21 | + never && true; |
| 22 | + true || never; |
| 23 | + true && never; |
| 24 | + for (int i = 0; never; i++) {} |
| 25 | + [for (int i = 0; never; i++) 3]; |
| 26 | + } |
| 27 | + { |
| 28 | + // Check that values of type `boolean` are usable as booleans. |
| 29 | + if (boolean) {} |
| 30 | + [if (boolean) 3]; |
| 31 | + boolean ? 3 : 4; |
| 32 | + while (boolean) {} |
| 33 | + do {} while (boolean); |
| 34 | + boolean || true; |
| 35 | + boolean && true; |
| 36 | + true || boolean; |
| 37 | + true && boolean; |
| 38 | + for (int i = 0; boolean; i++) {} |
| 39 | + [for (int i = 0; boolean; i++) 3]; |
| 40 | + } |
| 41 | + { |
| 42 | + // Check that values of type `dynamic` are usable as booleans. |
| 43 | + if (any) {} |
| 44 | + [if (any) 3]; |
| 45 | + any ? 3 : 4; |
| 46 | + while (any) {} |
| 47 | + do {} while (any); |
| 48 | + any || true; |
| 49 | + any && true; |
| 50 | + true || any; |
| 51 | + true && any; |
| 52 | + for (int i = 0; any; i++) {} |
| 53 | + [for (int i = 0; any; i++) 3]; |
| 54 | + } |
| 55 | + { |
| 56 | + // Check that values of type `Null` are not usable as booleans. |
| 57 | + if (nil) {} |
| 58 | + // ^^^ |
| 59 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 60 | + // [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 61 | + [if (nil) 3]; |
| 62 | + // ^^^ |
| 63 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 64 | + // [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 65 | + nil ? 3 : 4; |
| 66 | +// ^^^ |
| 67 | +// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 68 | +// [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 69 | + while (nil) {} |
| 70 | + // ^^^ |
| 71 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 72 | + // [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 73 | + do {} while (nil); |
| 74 | + // ^^^ |
| 75 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 76 | + // [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 77 | + nil || true; |
| 78 | +// ^^^ |
| 79 | +// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 80 | +// [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 81 | + nil && true; |
| 82 | +// ^^^ |
| 83 | +// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 84 | +// [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 85 | + true || nil; |
| 86 | + // ^^^ |
| 87 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 88 | + // [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 89 | + true && nil; |
| 90 | + // ^^^ |
| 91 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 92 | + // [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 93 | + for (int i = 0; nil; i++) {} |
| 94 | + // ^^^ |
| 95 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 96 | + // [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 97 | + [for (int i = 0; nil; i++) 3]; |
| 98 | + // ^^^ |
| 99 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 100 | + // [cfe] A value of type 'Null?' can't be assigned to a variable of type 'bool'. |
| 101 | + } |
| 102 | + { |
| 103 | + // Check that values of type `Object` are not usable as booleans. |
| 104 | + if (object) {} |
| 105 | + // ^^^^^^ |
| 106 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 107 | + // [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 108 | + [if (object) 3]; |
| 109 | + // ^^^^^^ |
| 110 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 111 | + // [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 112 | + object ? 3 : 4; |
| 113 | +// ^^^^^^ |
| 114 | +// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 115 | +// [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 116 | + while (object) {} |
| 117 | + // ^^^^^^ |
| 118 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 119 | + // [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 120 | + do {} while (object); |
| 121 | + // ^^^^^^ |
| 122 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 123 | + // [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 124 | + object || true; |
| 125 | +// ^^^^^^ |
| 126 | +// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 127 | +// [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 128 | + object && true; |
| 129 | +// ^^^^^^ |
| 130 | +// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 131 | +// [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 132 | + true || object; |
| 133 | + // ^^^^^^ |
| 134 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 135 | + // [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 136 | + true && object; |
| 137 | + // ^^^^^^ |
| 138 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 139 | + // [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 140 | + for (int i = 0; object; i++) {} |
| 141 | + // ^^^^^^ |
| 142 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 143 | + // [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 144 | + [for (int i = 0; object; i++) 3]; |
| 145 | + // ^^^^^^ |
| 146 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 147 | + // [cfe] A value of type 'Object' can't be assigned to a variable of type 'bool'. |
| 148 | + } |
| 149 | + { |
| 150 | + // Check that values of type `Object?` are not usable as booleans. |
| 151 | + if (objectOrNull) {} |
| 152 | + // ^^^^^^^^^^^^ |
| 153 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 154 | + // [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 155 | + [if (objectOrNull) 3]; |
| 156 | + // ^^^^^^^^^^^^ |
| 157 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 158 | + // [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 159 | + objectOrNull ? 3 : 4; |
| 160 | +// ^^^^^^^^^^^^ |
| 161 | +// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 162 | +// [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 163 | + while (objectOrNull) {} |
| 164 | + // ^^^^^^^^^^^^ |
| 165 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 166 | + // [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 167 | + do {} while (objectOrNull); |
| 168 | + // ^^^^^^^^^^^^ |
| 169 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 170 | + // [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 171 | + objectOrNull || true; |
| 172 | +// ^^^^^^^^^^^^ |
| 173 | +// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 174 | +// [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 175 | + objectOrNull && true; |
| 176 | +// ^^^^^^^^^^^^ |
| 177 | +// [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 178 | +// [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 179 | + true || objectOrNull; |
| 180 | + // ^^^^^^^^^^^^ |
| 181 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 182 | + // [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 183 | + true && objectOrNull; |
| 184 | + // ^^^^^^^^^^^^ |
| 185 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_OPERAND |
| 186 | + // [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 187 | + for (int i = 0; objectOrNull; i++) {} |
| 188 | + // ^^^^^^^^^^^^ |
| 189 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 190 | + // [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 191 | + [for (int i = 0; objectOrNull; i++) 3]; |
| 192 | + // ^^^^^^^^^^^^ |
| 193 | + // [analyzer] STATIC_TYPE_WARNING.NON_BOOL_CONDITION |
| 194 | + // [cfe] A value of type 'Object?' can't be assigned to a variable of type 'bool'. |
| 195 | + } |
| 196 | +} |
0 commit comments