mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Missing nullity annotations for immutable collection iterators #1810

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
ImmutableCollection and sub-interfaces declare to return an 
UnmodifiableIterator from their iterator() methods.

UnmodifiableIterator deprecates remove(), but does not redeclare next(). This 
means that the return type of next() is inferred to be @Nullable. (At least by 
Eclipse.)

This means that code like :

    for(String s: immutableSet) {
        // do something with s
    }

will warn about s being possibly null if used in a nonnull context, although 
the immutable versions of Collection, Set, and List at least cannot contain 
nulls. (This also affects ImmutableMaps, which return immutable views from 
several methods, and are not supposed to contain nulls either.)

I suggest adding a new sub-interface of UnmodifiableIterator, either 
ImmutableIterator or perhaps UnmodifiableNonnullIterator (and an analogous List 
version) which declares next() as @Nonnull, and using that instead for the 
relevant methods of the immutable collection.

(Unless, that is, things are the way they are now for some good reason that 
I’m not aware of, rather than an oversight.)

Original issue reported on code.google.com by bogd...@gmail.com on 21 Jul 2014 at 12:17

GoogleCodeExporter commented 9 years ago
I am somewhat inaccurately collapsing a bunch of nullability-annotation bugs 
into <https://code.google.com/p/guava-libraries/issues/detail?id=1812>. My 
apologies for the oversimplification.

Original comment by cpov...@google.com on 21 Jul 2014 at 8:05

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07