File: FlowLoop.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (162 lines) | stat: -rw-r--r-- 3,792 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import org.checkerframework.checker.nullness.qual.*;

public class FlowLoop {
    void simpleWhileLoop() {
        String s = "m";

        while (s != null) {
            s.toString();
            s = null;
        }
        // :: error: (dereference.of.nullable)
        s.toString(); // error
    }

    void whileConditionError() {
        String s = "m";

        // :: error: (dereference.of.nullable)
        while (s.toString() == "m") { // error
            s.toString();
            s = null;
        }
        s.toString();
    }

    void simpleForLoop() {
        for (String s = "m"; s != null; s = null) {
            s.toString();
        }
    }

    void forLoopConditionError() {
        for (String s = "m";
                // :: error: (dereference.of.nullable)
                s.toString() != "m"; // error
                s = null) {
            s.toString();
        }
    }

    class Link {
        Object val;
        @Nullable Link next;

        public Link(Object val, @Nullable Link next) {
            this.val = val;
            this.next = next;
        }
    }

    // Both dereferences of l should succeed
    void test(@Nullable Link in) {
        for (@Nullable Link l = in; l != null; l = l.next) {
            Object o;
            o = l.val;
        }
    }

    void multipleRuns() {
        String s = "m";
        while (true) {
            // :: error: (dereference.of.nullable)
            s.toString(); // error
            s = null;
        }
    }

    void multipleRunsDo() {
        String s = "m";
        do {
            // :: error: (dereference.of.nullable)
            s.toString(); // error
            s = null;
        } while (true);
    }

    void alwaysRunForLoop() {
        String s = "m";
        for (s = null; s != null; s = "m") {
            s.toString(); // ok
        }
        // :: error: (dereference.of.nullable)
        s.toString(); // error
    }

    public void badIterator() {
        Class<?> opt_doc1 = null;
        // :: error: (dereference.of.nullable)
        opt_doc1.getInterfaces();
        Class<?> opt_doc2 = null;
        // :: error: (dereference.of.nullable)
        for (Class<? extends @Nullable Object> fd : opt_doc2.getInterfaces()) {
            // empty loop body
        }
    }

    void testContinue(@Nullable Object o) {
        for (; ; ) {
            // :: error: (dereference.of.nullable)
            o.toString();
            if (true) continue;
        }
    }

    void testBreak(@Nullable Object o) {
        while (true) {
            // :: error: (dereference.of.nullable)
            o.toString();
            if (true) break;
        }
    }

    void testSimpleNull() {
        String r1 = null;
        while (r1 != null) ;
        // :: error: (dereference.of.nullable)
        r1.toString(); // error
    }

    void testMulticheckNull() {
        String r1 = null;
        while (r1 != null && r1.equals("m")) ;
        // :: error: (dereference.of.nullable)
        r1.toString(); // error
    }

    void testAssignInLoopSimple() {
        String r1 = "";
        while (r1 != null) {
            r1 = null;
        }
        // :: error: (dereference.of.nullable)
        r1.toString(); // error
    }

    void testAssignInLoopMulti() {
        String r1 = "";
        while (r1 != null && r1.isEmpty()) {
            r1 = null;
        }
        // :: error: (dereference.of.nullable)
        r1.toString(); // error
    }

    void testBreakWithCheck() {
        String s = null;
        while (true) {
            if (s == null) break;
            s.toString();
        }
    }

    void test1() {
        while (true) {
            String s = null;
            if (s == null) {
                return;
            }
            s.toString();
        }
    }
}