From 30f5c1b953f1c21a071b5fa4e075d673083cdcdb Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Wed, 4 Feb 2015 19:53:43 +0300 Subject: [PATCH] Reporting unnecessary safe calls on Java's @NotNull values --- .../load/kotlin/KotlinJvmCheckerProvider.kt | 10 ++++- .../nullabilityWarnings/safeCall.kt | 44 +++++++++++++++++++ .../nullabilityWarnings/safeCall.txt | 3 ++ .../checkers/JetDiagnosticsTestGenerated.java | 6 +++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/safeCall.kt create mode 100644 compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/safeCall.txt diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmCheckerProvider.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmCheckerProvider.kt index b9092056a66..2be1415c51c 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmCheckerProvider.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmCheckerProvider.kt @@ -201,11 +201,12 @@ public class JavaNullabilityWarningsChecker : AdditionalTypeChecker { safeAccess: Boolean, c: CallResolutionContext<*> ) { + val dataFlowValue = DataFlowValueFactory.createDataFlowValue(receiverArgument, c.trace.getBindingContext()) if (!safeAccess) { doCheckType( receiverArgument.getType(), receiverParameter.getType(), - DataFlowValueFactory.createDataFlowValue(receiverArgument, c.trace.getBindingContext()), + dataFlowValue, c.dataFlowInfo ) { expectedMustNotBeNull, @@ -222,5 +223,12 @@ public class JavaNullabilityWarningsChecker : AdditionalTypeChecker { } } + else { + if (c.dataFlowInfo.getNullability(dataFlowValue).canBeNull() + && receiverArgument.getType().mustNotBeNull() == NullabilityInformationSource.JAVA) { + c.trace.report(Errors.UNNECESSARY_SAFE_CALL.on(c.call.getCallOperationNode().getPsi(), receiverArgument.getType())) + + } + } } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/safeCall.kt b/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/safeCall.kt new file mode 100644 index 00000000000..bc6acd772f6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/safeCall.kt @@ -0,0 +1,44 @@ +// FILE: p/J.java +package p; + +import org.jetbrains.annotations.*; + +public class J { + @NotNull + public static J staticNN; + @Nullable + public static J staticN; + public static J staticJ; + + public void foo() {} +} + +// FILE: k.kt + +import p.* + +fun test() { + // @NotNull platform type + val platformNN = J.staticNN + // @Nullable platform type + val platformN = J.staticN + // platform type with no annotation + val platformJ = J.staticJ + + platformNN?.foo() + platformN?.foo() + platformJ?.foo() + + if (platformNN != null) { + platformNN?.foo() + } + + if (platformN != null) { + platformN?.foo() + } + + if (platformJ != null) { + platformJ?.foo() + } +} + diff --git a/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/safeCall.txt b/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/safeCall.txt new file mode 100644 index 00000000000..b8def46715c --- /dev/null +++ b/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/safeCall.txt @@ -0,0 +1,3 @@ +package + +internal fun test(): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 4eb1a34f535..ef6e8e64205 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -8640,6 +8640,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("safeCall.kt") + public void testSafeCall() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/safeCall.kt"); + doTest(fileName); + } + @TestMetadata("throw.kt") public void testThrow() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/throw.kt");