Warn on for-in-array range variable assignment in loop body

According to KT-21354, this should be a warning in 1.2 and before, and
no warning (with changed semantics) in 1.3 and later.
NB there are some false positives in this check.

 #KT-21354 In Progress
 #KT-21321 In Progress
This commit is contained in:
Dmitry Petrov
2017-12-04 13:50:28 +03:00
parent 7118a4bf80
commit 7a6f80606b
11 changed files with 335 additions and 1 deletions
@@ -0,0 +1,39 @@
// !LANGUAGE: -ProperForInArrayLoopRangeVariableAssignmentSemantic
// !DIAGNOSTICS: -UNUSED_VALUE
// SKIP_TXT
fun testObjectArray() {
var xs = arrayOf("a", "b", "c")
for (x in xs) {
println(x)
<!ASSIGNMENT_TO_ARRAY_LOOP_VARIABLE!>xs<!> = arrayOf("d", "e", "f")
}
}
fun testPrimitiveArray() {
var xs = intArrayOf(1, 2, 3)
for (x in xs) {
println(x)
<!ASSIGNMENT_TO_ARRAY_LOOP_VARIABLE!>xs<!> = intArrayOf(4, 5, 6)
}
}
var global = arrayOf("a", "b", "c")
fun testGlobalArray() {
for (x in global) {
println(x)
global = arrayOf("d", "e", "f")
}
}
fun testAssignmentNotInLoop() {
var xs = intArrayOf(1, 2, 3)
println(xs)
xs = intArrayOf(7, 8, 9)
for (x in xs) {
println(x)
}
xs = intArrayOf(4, 5, 6)
println(xs)
}