Refine Nullness for Kotlin functions returning Unit
Should have unspecified nullness like for Java void/Void. Closes gh-35420
This commit is contained in:
parent
e5b58effa3
commit
d2bdf11b39
|
@ -181,10 +181,10 @@ public enum Nullness {
|
||||||
|
|
||||||
public static Nullness forMethodReturnType(Method method) {
|
public static Nullness forMethodReturnType(Method method) {
|
||||||
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
|
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
|
||||||
if (function != null && function.getReturnType().isMarkedNullable()) {
|
if (function != null && ReflectJvmMapping.getJavaType(function.getReturnType()) != void.class) {
|
||||||
return Nullness.NULLABLE;
|
return (function.getReturnType().isMarkedNullable() ? Nullness.NULLABLE : Nullness.NON_NULL);
|
||||||
}
|
}
|
||||||
return Nullness.NON_NULL;
|
return Nullness.UNSPECIFIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Nullness forParameter(Executable executable, int parameterIndex) {
|
public static Nullness forParameter(Executable executable, int parameterIndex) {
|
||||||
|
|
|
@ -377,6 +377,13 @@ public class NullnessTests {
|
||||||
Assertions.assertThat(nullness).isEqualTo(Nullness.NULLABLE);
|
Assertions.assertThat(nullness).isEqualTo(Nullness.NULLABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void voidClassMethod() throws NoSuchMethodException {
|
||||||
|
var method = JSpecifyProcessor.class.getMethod("voidClassProcess");
|
||||||
|
var nullness = Nullness.forMethodReturnType(method);
|
||||||
|
Assertions.assertThat(nullness).isEqualTo(Nullness.UNSPECIFIED);
|
||||||
|
}
|
||||||
|
|
||||||
// Primitive types
|
// Primitive types
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -37,6 +37,13 @@ class NullnessKotlinTests {
|
||||||
Assertions.assertThat(nullness).isEqualTo(Nullness.NULLABLE)
|
Assertions.assertThat(nullness).isEqualTo(Nullness.NULLABLE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unitReturnType() {
|
||||||
|
val method = ::unit.javaMethod!!
|
||||||
|
val nullness = Nullness.forMethodReturnType(method)
|
||||||
|
Assertions.assertThat(nullness).isEqualTo(Nullness.UNSPECIFIED)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun nullableParameter() {
|
fun nullableParameter() {
|
||||||
val method = ::nullable.javaMethod!!
|
val method = ::nullable.javaMethod!!
|
||||||
|
@ -78,4 +85,7 @@ class NullnessKotlinTests {
|
||||||
@Suppress("unused_parameter")
|
@Suppress("unused_parameter")
|
||||||
fun nonNull(nonNull: String): String = "foo"
|
fun nonNull(nonNull: String): String = "foo"
|
||||||
|
|
||||||
|
fun unit() {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -37,5 +37,9 @@ public interface JSpecifyProcessor {
|
||||||
@NullMarked
|
@NullMarked
|
||||||
@NonNull String nonNullMarkedProcess();
|
@NonNull String nonNullMarkedProcess();
|
||||||
|
|
||||||
|
@NullMarked
|
||||||
void voidProcess();
|
void voidProcess();
|
||||||
|
|
||||||
|
@NullMarked
|
||||||
|
void voidClassProcess();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue