Migration: test interactions between null-shorting and new compound assignment diagnostics
Change-Id: Ia46aaf380fad1c65b59fff214217658cfcaee973
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/146000
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
diff --git a/pkg/nnbd_migration/test/fix_builder_test.dart b/pkg/nnbd_migration/test/fix_builder_test.dart
index 978a2c6..f33407d 100644
--- a/pkg/nnbd_migration/test/fix_builder_test.dart
+++ b/pkg/nnbd_migration/test/fix_builder_test.dart
@@ -1058,6 +1058,22 @@
visitSubexpression(findNode.booleanLiteral('true'), 'bool');
}
+ Future<void> test_compound_assignment_null_shorted_ok() async {
+ await analyze('''
+class C {
+ int/*!*/ x;
+}
+_f(C/*?*/ c) {
+ c?.x += 1;
+}
+''');
+ // Even though c?.x is nullable, it should not be a problem to use it as the
+ // LHS of a compound assignment, because null shorting will ensure that the
+ // assignment only happens if c is non-null.
+ var assignment = findNode.assignment('+=');
+ visitSubexpression(assignment, 'int?');
+ }
+
Future<void> test_compound_assignment_nullable_result_bad() async {
await analyze('''
abstract class C {
@@ -1974,6 +1990,22 @@
visitSubexpression(findNode.postfix('++'), 'int');
}
+ Future<void> test_post_increment_null_shorted_ok() async {
+ await analyze('''
+class C {
+ int/*!*/ x;
+}
+_f(C/*?*/ c) {
+ c?.x++;
+}
+''');
+ // Even though c?.x is nullable, it should not be a problem to use it as the
+ // target of a post-increment, because null shorting will ensure that the
+ // increment only happens if c is non-null.
+ var increment = findNode.postfix('++');
+ visitSubexpression(increment, 'int?');
+ }
+
Future<void> test_post_increment_nullable_result_bad() async {
await analyze('''
abstract class C {
@@ -2164,6 +2196,22 @@
visitSubexpression(findNode.prefix('++'), 'int');
}
+ Future<void> test_pre_increment_null_shorted_ok() async {
+ await analyze('''
+class C {
+ int/*!*/ x;
+}
+_f(C/*?*/ c) {
+ ++c?.x;
+}
+''');
+ // Even though c?.x is nullable, it should not be a problem to use it as the
+ // target of a pre-increment, because null shorting will ensure that the
+ // increment only happens if c is non-null.
+ var increment = findNode.prefix('++');
+ visitSubexpression(increment, 'int?');
+ }
+
Future<void> test_pre_increment_nullable_result_bad() async {
await analyze('''
abstract class C {