Warning for possible null value passed to a @NotNull parameter

Hi there,

Should IDEA issue a warning for passing a possible null value for a parameter
marked as NotNull? Or only if the value for the parameter comes from something
marked @Nullable should I expect the warning?

Thx,
Andrei

-



doSmth(props.get(randomKey)); //expecting warning here...

Where I already have

Map<String, String> props = ...

and

private void doSmth(@NotNull String s){
//some code
}


3 comments

Hi Andrei,

IDEA assumes non-annotated variables/methods as NotNull, which seems to be a good default
to avoid a lot of false positives with library methods. Actually, IDEA just assumes
nothing about unannotated element unless the control-flow indicates something else:

Object o = props.get(randomKey);
if (o == null) { ... }
doSmth(o); // Warning: o might be null

I think there's a request to change the default behavior and also to have some automated
support for annotating methods parameters and return values based on what could be null
and what shouldn't be null. Something that can tell me that in

void doSmth(String s) {
if (s.length() == 0) return;
}

the parameter s should be annotated as @NotNull to avoid trouble, while it would suggest for

void doSmth(String s) {
if (s == null) return;
if (s.length() == 0) return;
}

to annotate the parameter as @Nullable. Unfortunately, I can't find the requests right now :(

Sascha


Andrei Oprea wrote:

Hi there,

Should IDEA issue a warning for passing a possible null value for a
parameter marked as NotNull? Or only if the value for the parameter
comes from something marked @Nullable should I expect the warning?

Thx,
Andrei

-------------

doSmth(props.get(randomKey)); //expecting warning here...

Where I already have

Map<String, String> props = ...

and

private void doSmth(@NotNull String s){
//some code
}

0

Please sign in to leave a comment.