[FIR] Prevent replacing UNNECESSARY_SAFE_CALL with UNEXPECTED_SAFE_CALL

UNNECESSARY_SAFE_CALL is a warning,
UNEXPECTED_SAFE_CALL is an error, thus
it's a breaking change.

Also see KT-60695.

^KT-59860 Fixed


Merge-request: KT-MR-11210
Merged-by: Nikolay Lunyak <Nikolay.Lunyak@jetbrains.com>
This commit is contained in:
Nikolay Lunyak
2023-07-25 13:16:37 +00:00
committed by Space Team
parent cd073690fa
commit 57908eba77
10 changed files with 50 additions and 7 deletions
@@ -1245,6 +1245,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt");
}
@Test
@TestMetadata("unexpectedSafeCall.kt")
public void testUnexpectedSafeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt");
}
@Test
@TestMetadata("UnitByDefaultForFunctionTypes.kt")
public void testUnitByDefaultForFunctionTypes() throws Exception {
@@ -1245,6 +1245,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt");
}
@Test
@TestMetadata("unexpectedSafeCall.kt")
public void testUnexpectedSafeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt");
}
@Test
@TestMetadata("UnitByDefaultForFunctionTypes.kt")
public void testUnitByDefaultForFunctionTypes() throws Exception {
@@ -1245,6 +1245,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt");
}
@Test
@TestMetadata("unexpectedSafeCall.kt")
public void testUnexpectedSafeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt");
}
@Test
@TestMetadata("UnitByDefaultForFunctionTypes.kt")
public void testUnitByDefaultForFunctionTypes() throws Exception {
@@ -1245,6 +1245,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt");
}
@Test
@TestMetadata("unexpectedSafeCall.kt")
public void testUnexpectedSafeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt");
}
@Test
@TestMetadata("UnitByDefaultForFunctionTypes.kt")
public void testUnitByDefaultForFunctionTypes() throws Exception {
@@ -12,8 +12,13 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.diagnostics.reportOn
import org.jetbrains.kotlin.fir.expressions.FirSafeCallExpression
import org.jetbrains.kotlin.fir.expressions.calleeReference
import org.jetbrains.kotlin.fir.languageVersionSettings
import org.jetbrains.kotlin.fir.references.resolved
import org.jetbrains.kotlin.fir.references.toResolvedCallableSymbol
import org.jetbrains.kotlin.fir.resolve.fullyExpandedType
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.types.ConeErrorType
import org.jetbrains.kotlin.fir.types.canBeNull
import org.jetbrains.kotlin.fir.types.coneType
import org.jetbrains.kotlin.fir.types.isUnit
@@ -21,7 +26,7 @@ import org.jetbrains.kotlin.fir.types.isUnit
object FirUnnecessarySafeCallChecker : FirSafeCallExpressionChecker() {
override fun check(expression: FirSafeCallExpression, context: CheckerContext, reporter: DiagnosticReporter) {
val receiverType = expression.receiver.typeRef.coneType.fullyExpandedType(context.session)
if (receiverType.isUnit || expression.receiver.source?.elementType == KtNodeTypes.SUPER_EXPRESSION) {
if (expression.receiver.source?.elementType == KtNodeTypes.SUPER_EXPRESSION) {
reporter.reportOn(expression.source, FirErrors.UNEXPECTED_SAFE_CALL, context)
return
}
@@ -10,7 +10,7 @@ val s: String = "test"
// FILE: test.kt
fun ff() {
val a = <!NO_COMPANION_OBJECT!>Test<!><!UNEXPECTED_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>FOO<!>
val b = <!EXPRESSION_EXPECTED_PACKAGE_FOUND!>foo<!><!UNEXPECTED_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>s<!>
<!NO_COMPANION_OBJECT!>System<!><!UNEXPECTED_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>out<!>.println(a + b)
val a = <!NO_COMPANION_OBJECT!>Test<!><!UNNECESSARY_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>FOO<!>
val b = <!EXPRESSION_EXPECTED_PACKAGE_FOUND!>foo<!><!UNNECESSARY_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>s<!>
<!NO_COMPANION_OBJECT!>System<!><!UNNECESSARY_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>out<!>.println(a + b)
}
@@ -2,6 +2,6 @@
fun test() {
val b: Int
run { b = 1 }<!UNEXPECTED_SAFE_CALL!>?.<!>let {}
run { b = 1 }<!UNNECESSARY_SAFE_CALL!>?.<!>let {}
b.inc()
}
@@ -0,0 +1,8 @@
// FIR_IDENTICAL
// DIAGNOSTICS: -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE
// ISSUE: KT-59860
fun test() {
val b: Int
run { b = 1 }<!UNNECESSARY_SAFE_CALL!>?.<!>let {} // K1: UNNECESSARY_SAFE_CALL, K2: UNEXPECTED_SAFE_CALL
}
@@ -6,7 +6,7 @@ public class Test {
// FILE: test.kt
fun ff() {
val a = Test.FOO
val b = <!NO_COMPANION_OBJECT!>Test<!><!UNEXPECTED_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>FOO<!>
val b = <!NO_COMPANION_OBJECT!>Test<!><!UNNECESSARY_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>FOO<!>
System.out.println(a + b)
<!NO_COMPANION_OBJECT!>System<!><!UNEXPECTED_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>out<!>.println(a + b)
<!NO_COMPANION_OBJECT!>System<!><!UNNECESSARY_SAFE_CALL!>?.<!><!UNRESOLVED_REFERENCE!>out<!>.println(a + b)
}
@@ -1245,6 +1245,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt");
}
@Test
@TestMetadata("unexpectedSafeCall.kt")
public void testUnexpectedSafeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt");
}
@Test
@TestMetadata("UnitByDefaultForFunctionTypes.kt")
public void testUnitByDefaultForFunctionTypes() throws Exception {