[Assign plugin] Add a compiler plugin for overloading assign ('=') operator
This commit is contained in:
committed by
Dmitriy Novozhilov
parent
6052962f50
commit
09d6dfc8bf
@@ -0,0 +1,44 @@
|
||||
// !DIAGNOSTICS: -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE
|
||||
|
||||
annotation class ValueContainer
|
||||
|
||||
@ValueContainer
|
||||
data class StringProperty(var v: String) {
|
||||
fun assign(v: String) {
|
||||
this.v = v
|
||||
}
|
||||
fun assign(v: StringProperty) {
|
||||
this.v = v.get()
|
||||
}
|
||||
fun get(): String = v
|
||||
}
|
||||
|
||||
fun `should not work with local val for different type`() {
|
||||
val property = StringProperty("OK")
|
||||
<!VAL_REASSIGNMENT!>property<!> = <!TYPE_MISMATCH!>"Fail"<!>
|
||||
}
|
||||
|
||||
fun `should not work with local val for same type`() {
|
||||
val property = StringProperty("OK")
|
||||
<!VAL_REASSIGNMENT!>property<!> = StringProperty("Fail")
|
||||
}
|
||||
|
||||
fun `should not work with local var for different type`() {
|
||||
var property = StringProperty("OK")
|
||||
property = <!TYPE_MISMATCH!>"Fail"<!>
|
||||
}
|
||||
|
||||
fun `should work with local var for same type`() {
|
||||
var property = StringProperty("OK")
|
||||
property = StringProperty("Fail")
|
||||
}
|
||||
|
||||
fun `should not work with method parameters`() {
|
||||
fun m1(property: StringProperty): Unit {
|
||||
<!VAL_REASSIGNMENT!>property<!> = <!TYPE_MISMATCH!>"Fail"<!>
|
||||
}
|
||||
|
||||
fun m2(property: StringProperty): Unit {
|
||||
<!VAL_REASSIGNMENT!>property<!> = StringProperty("Fail")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user