FIR checker: report VAL_REASSIGNMENT on value parameter

This commit is contained in:
Jinseong Jeon
2021-04-26 00:38:34 -07:00
committed by TeamCityServer
parent 2486555208
commit 03f85bea8f
7 changed files with 19 additions and 13 deletions
@@ -12,13 +12,16 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.analysis.diagnostics.reportOn import org.jetbrains.kotlin.fir.analysis.diagnostics.reportOn
import org.jetbrains.kotlin.fir.declarations.FirPropertyAccessor import org.jetbrains.kotlin.fir.declarations.FirPropertyAccessor
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
import org.jetbrains.kotlin.fir.expressions.FirVariableAssignment import org.jetbrains.kotlin.fir.expressions.FirVariableAssignment
import org.jetbrains.kotlin.fir.languageVersionSettings import org.jetbrains.kotlin.fir.languageVersionSettings
import org.jetbrains.kotlin.fir.references.FirBackingFieldReference import org.jetbrains.kotlin.fir.references.FirBackingFieldReference
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
object FirValReassignmentChecker : FirVariableAssignmentChecker() { object FirValReassignmentChecker : FirVariableAssignmentChecker() {
override fun check(expression: FirVariableAssignment, context: CheckerContext, reporter: DiagnosticReporter) { override fun check(expression: FirVariableAssignment, context: CheckerContext, reporter: DiagnosticReporter) {
checkValReassignmentViaBackingField(expression, context, reporter) checkValReassignmentViaBackingField(expression, context, reporter)
checkValReassignmentOnValueParameter(expression, context, reporter)
} }
private fun checkValReassignmentViaBackingField( private fun checkValReassignmentViaBackingField(
@@ -40,4 +43,13 @@ object FirValReassignmentChecker : FirVariableAssignmentChecker() {
} }
} }
} }
private fun checkValReassignmentOnValueParameter(
expression: FirVariableAssignment,
context: CheckerContext,
reporter: DiagnosticReporter
) {
val valueParameter = (expression.lValue as? FirResolvedNamedReference)?.resolvedSymbol?.fir as? FirValueParameter ?: return
reporter.reportOn(expression.lValue.source, FirErrors.VAL_REASSIGNMENT, valueParameter.symbol, context)
}
} }
@@ -59,7 +59,7 @@ fun canBe(i0: Int, j: Int) {
var i = i0 var i = i0
(label@ i) = 34 (label@ i) = 34
(label@ j) = 34 //repeat for j (label@ <!VAL_REASSIGNMENT!>j<!>) = 34 //repeat for j
val a = A() val a = A()
(l@ a.a) = 3894 (l@ a.a) = 3894
@@ -69,7 +69,7 @@ fun canBe(i0: Int, j: Int) {
} }
fun canBe2(j: Int) { fun canBe2(j: Int) {
(label@ j) = 34 (label@ <!VAL_REASSIGNMENT!>j<!>) = 34
} }
class A() { class A() {
@@ -54,7 +54,7 @@ fun t2() {
class A() {} class A() {}
fun t4(a: A) { fun t4(a: A) {
a = A() <!VAL_REASSIGNMENT!>a<!> = A()
} }
// ------------------------------------------------ // ------------------------------------------------
@@ -188,7 +188,7 @@ class AnonymousInitializers(var a: String, val b: String) {
} }
fun reassignFunParams(a: Int) { fun reassignFunParams(a: Int) {
a = 1 <!VAL_REASSIGNMENT!>a<!> = 1
} }
open class Open(a: Int, w: Int) {} open class Open(a: Int, w: Int) {}
@@ -5,7 +5,7 @@ fun testPrefix() {
fun testPostfixWithCall(n: Nothing) { fun testPostfixWithCall(n: Nothing) {
operator fun Nothing.inc(): Nothing = this operator fun Nothing.inc(): Nothing = this
n++ <!VAL_REASSIGNMENT!>n<!>++
} }
fun testPostfixSpecial() { fun testPostfixSpecial() {
@@ -1,7 +0,0 @@
@Suppress("warnings")
class C {
fun foo(p: String??) {
// Make sure errors are not suppressed:
p = ""
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
@Suppress("warnings") @Suppress("warnings")
class C { class C {
fun foo(p: String??) { fun foo(p: String??) {
@@ -5,7 +5,7 @@
// TESTCASE NUMBER: 1 // TESTCASE NUMBER: 1
fun case_1(x: Class?) { fun case_1(x: Class?) {
x!! x!!
<!DEBUG_INFO_EXPRESSION_TYPE("Class? & Class")!>x<!>[if (true) {x=null;0} else 0] += <!DEBUG_INFO_EXPRESSION_TYPE("Class? & Class")!>x<!>[0] <!DEBUG_INFO_EXPRESSION_TYPE("Class? & Class")!>x<!>[if (true) {<!VAL_REASSIGNMENT!>x<!>=null;0} else 0] += <!DEBUG_INFO_EXPRESSION_TYPE("Class? & Class")!>x<!>[0]
<!DEBUG_INFO_EXPRESSION_TYPE("Class? & Class")!>x<!> <!DEBUG_INFO_EXPRESSION_TYPE("Class? & Class")!>x<!>
<!DEBUG_INFO_EXPRESSION_TYPE("Class? & Class")!>x<!>[0].inv() <!DEBUG_INFO_EXPRESSION_TYPE("Class? & Class")!>x<!>[0].inv()
} }