Skip to content

JSpecifyMode: Incomplete checking on reads from Iterable<@Nullable X> and @Nullable X[] #1253

@jeffrey-easyesi

Description

@jeffrey-easyesi

Explicit use of .iterator()/.next() is checked correctly (if Iterator is null-marked by LibraryModels), but the equivalent for-each loop is not:

void use(Integer x) {}
void f1(Iterable<@Nullable Integer> list) {
    for (var it = list.iterator(); it.hasNext(); ) { use(it.next()); }    // warning, it.next() is nullable
    for (var it = list.iterator(); it.hasNext(); ) { int x = it.next(); } // warning, it.next() is nullable

    for (Integer x : list) use(x);  // no warning
    for (int x : list) {}           // no warning
}

Also, I tested the analogous loops with an array, and found that there's no unboxing checking on either:

void f2(@Nullable Integer[] array) {
    for (int i = 0; i < array.length; i++) { use(array[i]); }    // warning, array[i] is nullable
    for (int i = 0; i < array.length; i++) { int x = array[i]; } // no warning
    
    for (Integer x : array) use(x); // warning, x is nullable
    for (int x : array) {}          // no warning
}

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugjspecifyRelated to support for jspecify standard (see jspecify.dev)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions