File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -976,6 +976,36 @@ class C {
976976 await _checkSingleFileChanges (content, expected);
977977 }
978978
979+ test_flow_analysis_complex () async {
980+ var content = '''
981+ int f(int x) {
982+ while (x == null) {
983+ x = g(x);
984+ }
985+ return x;
986+ }
987+ int g(int x) => x == null ? 1 : null;
988+ main() {
989+ f(null);
990+ }
991+ ''' ;
992+ // Flow analysis can tell that the loop only exits if x is non-null, so the
993+ // return type of `f` can remain `int`, and no null check is needed.
994+ var expected = '''
995+ int f(int? x) {
996+ while (x == null) {
997+ x = g(x);
998+ }
999+ return x;
1000+ }
1001+ int? g(int? x) => x == null ? 1 : null;
1002+ main() {
1003+ f(null);
1004+ }
1005+ ''' ;
1006+ await _checkSingleFileChanges (content, expected);
1007+ }
1008+
9791009 test_flow_analysis_simple () async {
9801010 var content = '''
9811011int f(int x) {
You can’t perform that action at this time.
0 commit comments