Why does FindBugs complain that .collect(toSet()) might return null?

Viewed 427

I have the following code, which as far as I can see it cannot return null:

import javax.annotation.Nonnull;

public static @Nonnull Set<String> foo() {
    return Arrays.asList("a","b","c").stream().collect(Collectors.toSet()); // line 166
}

But FindBugs complains with a NP_NONNULL_RETURN_VIOLATION error.

[INFO] MyClass.foo() may return null, 
   but is declared @Nonnull [MyClass, MyClass]
   Returned at MyClass.java:[line 166]
   Known null at MyClass.java:[line 166]
   NP_NONNULL_RETURN_VIOLATION

I have checked the documentation to "collect" and "toSet" and they don't mention anything about nulls, neither that they return them, nor that they don't. But I can't imagine a situation in which that returns null.

I was thinking maybe FindBugs just doesn't understand Java 8 stream features. But FindBugs otherwise does seem to understand Java 8 features (e.g. the @Nonnull annotation itself.)

I am using the FindBugs Maven Plug-in version 3.0.4, and my source depends on the FindBugs 3.0.1 in Maven. I understand these are the latest versions at the time of writing.

(The code may appear to be a bit pointless, it's just an example.)

0 Answers
Related