[FIR] Implement warnings for java nullability type mismatch

#KT-56989
This commit is contained in:
Kirill Rakhman
2023-09-20 15:34:48 +02:00
committed by Space Team
parent 1ecbc094ec
commit a6fdeeb7df
158 changed files with 741 additions and 2021 deletions
@@ -1,82 +0,0 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// JSR305_GLOBAL_REPORT: ignore
// FILE: MyErrorNonnull.java
import javax.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.annotation.meta.TypeQualifierNickname;
import javax.annotation.meta.When;
import kotlin.annotations.jvm.*;
@Documented
@TypeQualifierNickname
@Nonnull(when = When.ALWAYS)
@Retention(RetentionPolicy.RUNTIME)
@UnderMigration(status = MigrationStatus.STRICT)
public @interface MyErrorNonnull {
}
// FILE: MyWarnNonnull.java
import javax.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.annotation.meta.TypeQualifierNickname;
import javax.annotation.meta.When;
import kotlin.annotations.jvm.*;
@Documented
@TypeQualifierNickname
@Nonnull(when = When.ALWAYS)
@Retention(RetentionPolicy.RUNTIME)
@UnderMigration(status = MigrationStatus.WARN)
public @interface MyWarnNonnull {
}
// FILE: MyIgnoreNonnull.java
import javax.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.annotation.meta.TypeQualifierNickname;
import javax.annotation.meta.When;
import kotlin.annotations.jvm.*;
@Documented
@TypeQualifierNickname
@Nonnull(when = When.ALWAYS)
@Retention(RetentionPolicy.RUNTIME)
@UnderMigration(status = MigrationStatus.IGNORE)
public @interface MyIgnoreNonnull {
}
// FILE: A.java
public class A {
public void foo(@MyErrorNonnull String bar) {}
public void foo2(@MyWarnNonnull String bar) {}
public void foo3(@MyIgnoreNonnull String bar) {}
public void foo4(@MyMigrationNonnull String bar) {}
}
// FILE: main.kt
fun main(a: A) {
a.foo("")
a.foo(<!NULL_FOR_NONNULL_TYPE!>null<!>)
a.foo2("")
a.foo2(null)
a.foo3("")
a.foo3(null)
a.foo4("")
a.foo4(null)
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// JSR305_GLOBAL_REPORT: ignore
@@ -45,7 +45,7 @@ fun main(a: A) {
a.foo2("", null)?.length
a.foo2("", null).length
a.foo2(null, "").length
a.foo2(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>, "").length
a.bar2().length
a.bar2()!!.length
@@ -45,7 +45,7 @@ fun main(a: A) {
a.foo2("", null)?.length
a.foo2("", null)<!UNSAFE_CALL!>.<!>length
a.foo2(null, "")<!UNSAFE_CALL!>.<!>length
a.foo2(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>, "")<!UNSAFE_CALL!>.<!>length
a.bar2().length
a.bar2()!!.length
@@ -1,76 +0,0 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// JSR305_GLOBAL_REPORT: warn
// JSR305_MIGRATION_REPORT: strict
// JSR305_SPECIAL_REPORT: MyNonnull:ignore, MySuperNull:strict
// FILE: MySuperNull.java
import javax.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.annotation.meta.TypeQualifierNickname;
import javax.annotation.meta.When;
@Documented
@TypeQualifierNickname
@Nonnull(when = When.MAYBE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MySuperNull {
}
// FILE: A.java
import javax.annotation.*;
public class A {
@MyMigrationNullable public String field = null;
@MyMigrationNullable
public String foo(@MyMigrationNonnull String x, CharSequence y) {
return "";
}
@MyMigrationNonnull
public String bar() {
return "";
}
@MyNullable public String field2 = null;
@MyNullable
public String foo2(@MyNonnull String x, CharSequence y) {
return "";
}
@MyNonnull
public String bar2() {
return "";
}
@MySuperNull public String field3 = null;
}
// FILE: main.kt
fun main(a: A) {
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.foo2("", null)?.length
a.foo2("", null).length
a.foo2(null, "").length
a.bar2().length
a.bar2()!!.length
a.field2?.length
a.field2.length
a.field3?.length
a.field3<!UNSAFE_CALL!>.<!>length
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// JSR305_GLOBAL_REPORT: warn
// JSR305_MIGRATION_REPORT: strict
@@ -1,55 +0,0 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// JSR305_GLOBAL_REPORT: strict
// JSR305_MIGRATION_REPORT: ignore
// JSR305_SPECIAL_REPORT: MyNullable:warn, MyMigrationNonnull:strict
// FILE: A.java
import javax.annotation.*;
public class A {
@MyMigrationNullable public String field = null;
@MyMigrationNullable
public String foo(@MyMigrationNonnull String x, CharSequence y) {
return "";
}
@MyMigrationNonnull
public String bar() {
return "";
}
@MyNullable public String field2 = null;
@MyNullable
public String foo2(@MyNonnull String x, CharSequence y) {
return "";
}
@MyNonnull
public String bar2() {
return "";
}
}
// FILE: main.kt
fun main(a: A) {
a.foo("", null)?.length
a.foo("", null).length
a.foo(<!NULL_FOR_NONNULL_TYPE!>null<!>, "").length
a.bar().length
a.bar()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
a.field?.length
a.field.length
a.foo2("", null)?.length
a.foo2("", null).length
a.foo2(<!NULL_FOR_NONNULL_TYPE!>null<!>, "").length
a.bar2().length
a.bar2()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
a.field2?.length
a.field2.length
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// JSR305_GLOBAL_REPORT: strict
// JSR305_MIGRATION_REPORT: ignore
@@ -35,7 +35,7 @@ public class A {
fun main(a: A) {
a.foo("", null)?.length
a.foo("", null)<!UNSAFE_CALL!>.<!>length
a.foo(null, "")<!UNSAFE_CALL!>.<!>length
a.foo(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>, "")<!UNSAFE_CALL!>.<!>length
a.bar().length
a.bar()!!.length
@@ -1,85 +0,0 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// JSR305_GLOBAL_REPORT: warn
// JSR305_MIGRATION_REPORT: strict
// FILE: A.java
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
public class A {
@MyNullable
public String foo() { return ""; }
@MyMigrationNullable
public String foo2() { return ""; }
@Nullable
public String foo3() { return ""; }
public String foo4() { return ""; }
public void bar(@MyNonnull String baz) { }
public void bar2(@MyMigrationNonnull String baz) { }
public void bar3(@Nonnull String baz) {}
public void bar4(String baz) {}
}
// FILE: B.java
public class B extends A {
@MyMigrationNullable
public String foo() { return ""; }
@MyNullable
public String foo2() { return ""; }
@MyNullable
public String foo3() { return ""; }
@MyNullable
public String foo4() { return ""; }
public void bar(@MyMigrationNonnull String baz) { }
public void bar2(@MyNonnull String baz) { }
public void bar3(@MyNonnull String baz) {}
public void bar4(@MyNonnull String baz) {}
}
// FILE: C.java
public class C extends A {
@MyMigrationNullable
public String foo4() { return ""; }
public void bar4(@MyMigrationNonnull String baz) {}
}
// FILE: main.kt
fun main(b: B, c: C) {
b.foo()<!UNSAFE_CALL!>.<!>length
b.foo()?.length
b.foo2()<!UNSAFE_CALL!>.<!>length
b.foo2()?.length
b.foo3()<!UNSAFE_CALL!>.<!>length
b.foo3()?.length
b.foo4().length
b.foo4()?.length
b.bar(<!NULL_FOR_NONNULL_TYPE!>null<!>)
b.bar("")
b.bar2(<!NULL_FOR_NONNULL_TYPE!>null<!>)
b.bar2("")
b.bar3(<!NULL_FOR_NONNULL_TYPE!>null<!>)
b.bar3("")
b.bar4(null)
b.bar4("")
c.foo4()<!UNSAFE_CALL!>.<!>length
c.foo4()?.length
c.bar4(<!NULL_FOR_NONNULL_TYPE!>null<!>)
c.bar4("")
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// JSR305_GLOBAL_REPORT: warn
// JSR305_MIGRATION_REPORT: strict
@@ -34,14 +34,14 @@ public class A {
// FILE: main.kt
fun main(a: A) {
a.foo("", null)?.length
a.foo("", null).length
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()!!.length
a.field?.length
a.field.length
<!RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a.field<!>.length
a.foo2("", null)?.length
a.foo2("", null).length