Open EotT123 opened 1 year ago
Yep, these NPEs should be suppressed, great idea!
While technically true, this would make extension methods behave different from normal methods when it comes to nullability.
I think it would be better to invest into null-safe operators like ?.
for null-safe method calls or ??
to provide a default value when the object is null.
In Kotlin you'd have to mark a receiver specifically to be nullable, if you want to be allowed to call an extension method on a variable that contains the null value. This also adds clarity, since now you know if it's expected if you're allowed to use null in the call or if you'd need to use the ?.
operator to make it a null-safe call.
As an example where there's a difference between nullable extension methods called with ?.
and .
:
public static String myToString(@This @Nullable Object thiz) {
if (thiz == null) return "null value";
return thiz.toString();
}
public static final main() {
String foo = "not null value";
String bar = null;
String fooString1 = foo.myToString(); // returns "not null value"
String fooString2 = foo?.myToString(); // returns "not null value"
String barString1 = bar.myToString(); // returns "null value"
String barString2 = bar?.myToString(); // returns null
}
So for consistency with normal methods, I think the following would be preferred if the receiver is not specified to be nullable:
public static String myToString(@This Object thiz) { // note the absence of the nullable annotation
return thiz.toString();
}
public static final main() {
String foo = "not null value";
String bar = null;
String fooString1 = foo.myToString(); // returns "not null value"
String fooString2 = foo?.myToString(); // returns "not null value"
String barString1 = bar.myToString(); // throws NullPointerException
String barString2 = bar?.myToString(); // returns null
}
Describe the bug Extension methods are a great way to deal with nullness, eg see the example here:
This works great. However, when using a value returned by a method annotated with
@Nullable
, IntelliJ warns me:Method invocation 'xxx' may produce 'NullPointerException'
.Eg:
This is clearly not the case: Even if the value is null, it is still correctly handled, and no NPE is thrown.
To Reproduce Steps to reproduce the behavior:
MyObject
) of a method with@Nullable
MyObject
), which accepts a@Nullable MyObject
@Nullable
value.Expected behavior IntelliJ should not warn me about a potential NPE
Desktop (please complete the following information):