[Suggestion] "Unused" Reassignment of delegated local variables shouldn't issue a warning

A common mistake in code might be to reassign a local variable and then not use it afterwards. Whenever that happens, the compiler can say with great certainty that this assignment could as well be removed. That is why you usually get a warning in such a situation.

Example:

fun test() {
    var i = 5
    val j = 3
    println(i + j)
    i = 7          // Warning here: Unused re-assignment of 'i'
    println(j)
}

However, in some cases such a reassignment might actually not be useless, that is in the case of local variable delegation. Property / Local Variable Delegation is a mechanism to basically proxy all accesses of a property or local variable.

Example:

class IntDelegate(private var value: Int) {
    operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
        println("int variable was retrieved")
        return value
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
        println("int variable was reassigned")
        this.value = value
    }
}
fun test() {
    var i by IntDelegate(5)
    println(i)              // will print "int variable was retrieved", then "5"
    i = 4                   // will print "int variable was reassigned"
                            // but also, at this spot, a warning will be issued 
}

(See Delegated properties | Kotlin)

You can see that in the case of delegated variables, a reassignment might have (desired) side effects, so a reassignment at the end of a function might not be unnecessary.

The problem is that you’ll still get the above-mentioned warning. I think the inspection should be changed so that the warning is only issued for local variables that are not delegated.

What are your thoughts on this?

Seems like a false positive.

There are probably more similar false positives when using delegated local properties. For example, if we assign a delegated val to another val it is detected as redundant and IDE suggest to inline it. This is also wrong (but it was reported already).