File: Issue1847.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 (31 lines) | stat: -rw-r--r-- 1,303 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
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.KeyFor;

public class Issue1847 {
    final Map<String, String> map = new HashMap<>();

    public void test() {
        // Should give null error here:
        // :: error: (dereference.of.nullable)
        withLookup((String myVar) -> map.get(myVar).length());
        for (Iterator<Entry<@KeyFor("map") String, String>> iterator = map.entrySet().iterator();
                iterator.hasNext(); ) {
            Entry<@KeyFor("map") String, String> entry = iterator.next();
            // Problem is that myVar gets inferred as @KeyFor("map") here,
            // and this variable is not distinguished from the lambda variables of the same name,
            // even though their scopes do not overlap and they are different variables.
            // Change this variable name to myVar2 and you will see the null errors on the lambdas:
            String myVar = entry.getKey();
        }

        // Should also give null error here:
        // :: error: (dereference.of.nullable)
        withLookup(myVar -> map.get(myVar).length());
    }

    public void withLookup(Function<String, Integer> getFromMap) {}
}