Files
sdk/tests/language/null_aware_elements/flow_analysis_test.dart
T
Chloe Stefantsova 253e715f83 [analyzer] Report warnings on expressions with non-nullable types
Part of https://github.com/dart-lang/sdk/issues/56836

Change-Id: I3fcdceff667f371fcf4b66c12bd16e2f34e6446c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391621
Reviewed-by: Keerti Parthasarathy <keertip@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
2024-10-25 10:08:04 +00:00

84 lines
1.7 KiB
Dart

// Copyright (c) 2024, 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.
// SharedOptions=--enable-experiment=null-aware-elements
import '../static_type_helper.dart';
test1(String a, num x) {
<dynamic, dynamic>{?a: (x as int)};
// ^
// [analyzer] STATIC_WARNING.INVALID_NULL_AWARE_OPERATOR
x..expectStaticType<Exactly<int>>();
}
test2(String? a, num x) {
<dynamic, dynamic>{?a: (x as int)};
x..expectStaticType<Exactly<num>>();
}
test3(String a, bool b, num x) {
if (b) {
x as int;
} else {
<dynamic, dynamic>{?a: (throw 0)};
// ^
// [analyzer] STATIC_WARNING.INVALID_NULL_AWARE_OPERATOR
// Unreachable.
}
x..expectStaticType<Exactly<int>>();
}
test4(String? a, bool b, num x) {
if (b) {
x as int;
} else {
<dynamic, dynamic>{?a: (throw 0)};
// Reachable.
}
x..expectStaticType<Exactly<num>>();
}
test5(String? a) {
return <dynamic, dynamic>{?a: a..expectStaticType<Exactly<String>>()};
}
test6(String? a) {
return <dynamic, dynamic>{a: a..expectStaticType<Exactly<String?>>()};
}
expectThrows(void Function() f) {
bool hasThrown = true;
try {
f();
hasThrown = false;
} catch (e) {}
if (!hasThrown) {
throw "Expected the function to throw.";
}
}
main() {
test1("", 0);
test2("", 0);
test2(null, 0);
test3("", true, 0);
expectThrows(() => test3("", false, 0));
test4("", true, 0);
expectThrows(() => test4("", false, 0));
test4(null, true, 0);
test4(null, false, 0);
test5(null);
test5("");
test6(null);
test5("");
}