Reporting unnecessary safe calls on Java's @NotNull values
This commit is contained in:
+9
-1
@@ -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()))
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<!UNNECESSARY_SAFE_CALL!>?.<!>foo()
|
||||
platformN?.foo()
|
||||
platformJ?.foo()
|
||||
|
||||
if (platformNN != null) {
|
||||
platformNN<!UNNECESSARY_SAFE_CALL!>?.<!>foo()
|
||||
}
|
||||
|
||||
if (platformN != null) {
|
||||
platformN<!UNNECESSARY_SAFE_CALL!>?.<!>foo()
|
||||
}
|
||||
|
||||
if (platformJ != null) {
|
||||
platformJ<!UNNECESSARY_SAFE_CALL!>?.<!>foo()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
package
|
||||
|
||||
internal fun test(): kotlin.Unit
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user