Support rxjava 3 nullability annotations

This commit is contained in:
Victor Petukhov
2021-06-02 17:23:04 +03:00
parent 6d3badb2cd
commit f46dc713d7
11 changed files with 305 additions and 2 deletions

View File

@@ -0,0 +1,41 @@
// FILE: A.java
import io.reactivex.rxjava3.annotations.*;
public class A<T> {
@Nullable public String field = null;
@Nullable
public String foo(@NonNull String x, @Nullable CharSequence y) {
return "";
}
@NonNull
public String bar() {
return "";
}
@Nullable
public T baz(@NonNull T x) { return x; }
}
// FILE: main.kt
fun main(a: A<String>, a1: A<String?>) {
a.foo("", null)?.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.foo("", null)<!>.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.foo(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>, "")<!>.length
a.bar().length
a.bar()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
a.field?.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.field<!>.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.baz("")<!>.length
a.baz("")?.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.baz(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>)<!>.length
a1.baz("")!!.length
a1.baz(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>)!!.length
}

View File

@@ -0,0 +1,43 @@
// NULLABILITY_ANNOTATIONS: @io.reactivex.rxjava3.annotations:strict
// FILE: A.java
import io.reactivex.rxjava3.annotations.*;
public class A<T> {
@Nullable public String field = null;
@Nullable
public String foo(@NonNull String x, @Nullable CharSequence y) {
return "";
}
@NonNull
public String bar() {
return "";
}
@Nullable
public T baz(@NonNull T x) { return x; }
}
// FILE: main.kt
fun main(a: A<String>, a1: A<String?>) {
a.foo("", null)?.length
a.foo("", null)<!UNSAFE_CALL!>.<!>length
a.foo(<!NULL_FOR_NONNULL_TYPE!>null<!>, "")<!UNSAFE_CALL!>.<!>length
a.bar().length
a.bar()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
a.field?.length
a.field<!UNSAFE_CALL!>.<!>length
a.baz("")<!UNSAFE_CALL!>.<!>length
a.baz("")?.length
a.baz(<!NULL_FOR_NONNULL_TYPE!>null<!>)<!UNSAFE_CALL!>.<!>length
a1.baz("")!!.length
a1.baz(<!NULL_FOR_NONNULL_TYPE!>null<!>)!!.length
}

View File

@@ -0,0 +1,43 @@
// NULLABILITY_ANNOTATIONS: @io.reactivex.rxjava3.annotations:ignore
// FILE: A.java
import io.reactivex.rxjava3.annotations.*;
public class A<T> {
@Nullable public String field = null;
@Nullable
public String foo(@NonNull String x, @Nullable CharSequence y) {
return "";
}
@NonNull
public String bar() {
return "";
}
@Nullable
public T baz(@NonNull T x) { return x; }
}
// FILE: main.kt
fun main(a: A<String>, a1: A<String?>) {
a.foo("", null)?.length
a.foo("", null).length
a.foo(null, "").length
a.bar().length
a.bar()!!.length
a.field?.length
a.field.length
a.baz("").length
a.baz("")?.length
a.baz(null).length
a1.baz("")!!.length
a1.baz(null)!!.length
}

View File

@@ -0,0 +1,43 @@
// NULLABILITY_ANNOTATIONS: @io.reactivex.rxjava3.annotations:warn
// FILE: A.java
import io.reactivex.rxjava3.annotations.*;
public class A<T> {
@Nullable public String field = null;
@Nullable
public String foo(@NonNull String x, @Nullable CharSequence y) {
return "";
}
@NonNull
public String bar() {
return "";
}
@Nullable
public T baz(@NonNull T x) { return x; }
}
// FILE: main.kt
fun main(a: A<String>, a1: A<String?>) {
a.foo("", null)?.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.foo("", null)<!>.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.foo(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>, "")<!>.length
a.bar().length
a.bar()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
a.field?.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.field<!>.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.baz("")<!>.length
a.baz("")?.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.baz(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>)<!>.length
a1.baz("")!!.length
a1.baz(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>)!!.length
}

View File

@@ -98,6 +98,30 @@ public class ForeignAnnotationsCompiledJavaTestGenerated extends AbstractForeign
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava.kt");
}
@Test
@TestMetadata("rxjava3Default.kt")
public void testRxjava3Default() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Default.kt");
}
@Test
@TestMetadata("rxjava3Errors.kt")
public void testRxjava3Errors() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Errors.kt");
}
@Test
@TestMetadata("rxjava3Ignore.kt")
public void testRxjava3Ignore() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Ignore.kt");
}
@Test
@TestMetadata("rxjava3Warnings.kt")
public void testRxjava3Warnings() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Warnings.kt");
}
@Nested
@TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305")
@TestDataPath("$PROJECT_ROOT")

View File

@@ -98,6 +98,30 @@ public class ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated exte
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava.kt");
}
@Test
@TestMetadata("rxjava3Default.kt")
public void testRxjava3Default() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Default.kt");
}
@Test
@TestMetadata("rxjava3Errors.kt")
public void testRxjava3Errors() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Errors.kt");
}
@Test
@TestMetadata("rxjava3Ignore.kt")
public void testRxjava3Ignore() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Ignore.kt");
}
@Test
@TestMetadata("rxjava3Warnings.kt")
public void testRxjava3Warnings() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Warnings.kt");
}
@Nested
@TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305")
@TestDataPath("$PROJECT_ROOT")

View File

@@ -98,6 +98,30 @@ public class ForeignAnnotationsSourceJavaTestGenerated extends AbstractForeignAn
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava.kt");
}
@Test
@TestMetadata("rxjava3Default.kt")
public void testRxjava3Default() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Default.kt");
}
@Test
@TestMetadata("rxjava3Errors.kt")
public void testRxjava3Errors() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Errors.kt");
}
@Test
@TestMetadata("rxjava3Ignore.kt")
public void testRxjava3Ignore() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Ignore.kt");
}
@Test
@TestMetadata("rxjava3Warnings.kt")
public void testRxjava3Warnings() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Warnings.kt");
}
@Nested
@TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305")
@TestDataPath("$PROJECT_ROOT")

View File

@@ -35,7 +35,8 @@ val NULLABLE_ANNOTATIONS = listOf(
FqName("edu.umd.cs.findbugs.annotations.CheckForNull"),
FqName("edu.umd.cs.findbugs.annotations.Nullable"),
FqName("edu.umd.cs.findbugs.annotations.PossiblyNull"),
FqName("io.reactivex.annotations.Nullable")
FqName("io.reactivex.annotations.Nullable"),
FqName("io.reactivex.rxjava3.annotations.Nullable")
)
val JAVAX_NONNULL_ANNOTATION = FqName("javax.annotation.Nonnull")
@@ -51,7 +52,8 @@ val NOT_NULL_ANNOTATIONS = listOf(
FqName("org.eclipse.jdt.annotation.NonNull"),
FqName("org.checkerframework.checker.nullness.qual.NonNull"),
FqName("lombok.NonNull"),
FqName("io.reactivex.annotations.NonNull")
FqName("io.reactivex.annotations.NonNull"),
FqName("io.reactivex.rxjava3.annotations.NonNull")
)
val COMPATQUAL_NULLABLE_ANNOTATION = FqName("org.checkerframework.checker.nullness.compatqual.NullableDecl")

View File

@@ -37,6 +37,11 @@ val nullabilityAnnotationSettings = mapOf(
sinceVersion = KotlinVersion(1, 6),
reportLevelAfter = ReportLevel.STRICT
),
FqName("io.reactivex.rxjava3.annotations") to JavaNullabilityAnnotationsStatus(
reportLevelBefore = ReportLevel.WARN,
sinceVersion = KotlinVersion(1, 7),
reportLevelAfter = ReportLevel.STRICT
),
)
private val jsr305Settings = JavaNullabilityAnnotationsStatus(

View File

@@ -0,0 +1,27 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.rxjava3.annotations;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.CLASS;
import java.lang.annotation.*;
/**
* Indicates that a field/parameter/variable/type parameter/return type is never null.
*/
@Documented
@Target(value = {FIELD, METHOD, PARAMETER, LOCAL_VARIABLE, TYPE_PARAMETER, TYPE_USE})
@Retention(value = CLASS)
public @interface NonNull { }

View File

@@ -0,0 +1,27 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.rxjava3.annotations;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.CLASS;
import java.lang.annotation.*;
/**
* Indicates that a field/parameter/variable/type parameter/return type may be null.
*/
@Documented
@Target(value = {FIELD, METHOD, PARAMETER, LOCAL_VARIABLE, TYPE_PARAMETER, TYPE_USE})
@Retention(value = CLASS)
public @interface Nullable { }