-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Nullness] false positive for BasicMultimap #795
Comments
Thanks for the report! This is actually desired behavior, caused by our goal of being sound and preventing all possible NPEs. Note that the parameter to As you note, you could add a Another way to relax this constraint is to use the stub file at: https://github.com/eisop/checker-framework/blob/master/checker/src/main/java/org/checkerframework/checker/nullness/collection-object-parameters-may-be-null.astub Also see: |
Another alternative (which would be somewhat against typical Java style, though I like it :)) would be to declare your field type as |
Thank you for your answers. Here is fixed (proof) initial example that pass nullness check: import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
interface BasicMultimap<K extends @NonNull Object, V extends @NonNull Object> {
@Nullable List<V> get(K key);
void put(K key, V value);
}
class BasicMultimapImpl<K extends @NonNull Object, V extends @NonNull Object> implements BasicMultimap<K, V> {
private final Map<K, List<V>> delegate = new HashMap<>();
public void put(K key, V value) {
delegate.computeIfAbsent(key, ignored -> new ArrayList<>()).add(value);
}
public @Nullable List<V> get(K key) {
return delegate.get(key);
}
} A couple of newcomer questions:
|
Re 1: The Re 2: I'm not sure what you mean with "suffer from this verbosity"... the Welcome to static null checking! Feel free to open issues or post a discussion and we'll try to help. |
An intro of "Nullness Checker" section will reference "Generics and polymorphism" section to mention that defaults in the latter differs from defaults for a non-generic parameter, e.g. a Method argument. That should help a new reader to catch the difference sooner. resolves: eisop#795
Thank you for such detailed comment.
Another my very biased point is, for historical perspective we have a paradigm shift. From "Everything might be null by default" in Good Old Java to "Every argument is Every casual user of the CF lives somewhere in between Good Old Java approach and Rust approach. A user might think: "Whatever default your excellent framework has, I would like to have an option to override them for this or that particular module of my project." It would be nice to have a command-line flag to say "All type parameters here are non-null by default" or even "Both arguments and type parameters are null by default". In my particular case the former option would reduce verbosity, as just |
Inputs
Live demo of the example
Output
An error at line 23, column 29:
Expectation
One expects no error here.
It is not quite clear where to add what annotation (
@NonNull
, whatever ?) except@SuppressWarnings("nullness")
above the line 23 to get rid of the error.The text was updated successfully, but these errors were encountered: