So far the @NotNull annotation can be applied to pretty much any element. And it is an inspection error to assign an element of type @Nullable (or maybe unknown type, see
http://www.jetbrains.net/jira/browse/IDEA-2087
).
But what about the situation where we assign a value to a variable where the assignment doesn't come from a method return value, a field or a local variable?
void doTest() {
// We have flagged the collection, but not its contents
@NotNull Collection<String> c = new ArrayList<String> ();
c.add ("Test");
c.add (null); // Oops
// How can we be sure that all the elements of c are not null?
for (@NotNull String s : c) {
// Not flagged
doTest2 (s);
}
}
void doTest2 (@NotNull String s) {
// Not flagged, and I don't test for null
if (s.equals ("Test")) {
// ...
}
}
Do we maybe need a subsidiary annotation, something like @IteratorNeverReturnsNullInAnyCaseWhatsoever (or maybe something a bit shorter
?
OK, this isn't really a bug, but I was just playing around and I thought I would submit this case for consideration.