KT-1273 Unused assignment - should highlight assignment instead of expression

#KT-1273 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-05-16 17:31:56 +03:00
parent 189286efee
commit 7edff172a2
26 changed files with 79 additions and 69 deletions
@@ -176,7 +176,7 @@ fun testFunctionLiterals() {
val <!UNUSED_VARIABLE!>endsWithAssignment<!>: () -> Int = {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!> = 1
<!EXPECTED_TYPE_MISMATCH!>x = <!UNUSED_VALUE!>333<!><!>
<!EXPECTED_TYPE_MISMATCH!><!UNUSED_VALUE!>x =<!> 333<!>
}
val <!UNUSED_VARIABLE!>endsWithReAssignment<!>: () -> Int = {
@@ -186,13 +186,13 @@ fun testFunctionLiterals() {
val <!UNUSED_VARIABLE!>endsWithFunDeclaration<!> : () -> String = {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!> = 1
x = <!UNUSED_VALUE!>333<!>
<!UNUSED_VALUE!>x =<!> 333
<!EXPECTED_TYPE_MISMATCH!>fun meow() : Unit {}<!>
}
val <!UNUSED_VARIABLE!>endsWithObjectDeclaration<!> : () -> Int = {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!> = 1
x = <!UNUSED_VALUE!>333<!>
<!UNUSED_VALUE!>x =<!> 333
<!LOCAL_OBJECT_NOT_ALLOWED, EXPECTED_TYPE_MISMATCH!>object A<!> {}
}
@@ -12,7 +12,7 @@ fun testIncDec() {
x = <!UNUSED_CHANGED_VALUE!>x++<!>
x = <!UNUSED_CHANGED_VALUE!>x--<!>
x = ++x
x = <!UNUSED_VALUE!>--x<!>
<!UNUSED_VALUE!>x =<!> --x
}
class WrongIncDec() {
@@ -42,5 +42,5 @@ fun testUnitIncDec() {
x = <!UNUSED_CHANGED_VALUE!>x<!INC_DEC_SHOULD_NOT_RETURN_UNIT!>++<!><!>
x = <!UNUSED_CHANGED_VALUE!>x<!INC_DEC_SHOULD_NOT_RETURN_UNIT!>--<!><!>
x = <!INC_DEC_SHOULD_NOT_RETURN_UNIT!>++<!>x
x = <!UNUSED_VALUE!><!INC_DEC_SHOULD_NOT_RETURN_UNIT!>--<!>x<!>
<!UNUSED_VALUE!>x =<!> <!INC_DEC_SHOULD_NOT_RETURN_UNIT!>--<!>x
}
@@ -53,16 +53,16 @@ fun cannotBe() {
fun canBe(i0: Int, j: Int) {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>i<!> = i0
(label@ i) = <!UNUSED_VALUE!>34<!>
<!UNUSED_VALUE!>(label@ i) =<!> 34
(label@ <!VAL_REASSIGNMENT!>j<!>) = <!UNUSED_VALUE!>34<!> //repeat for j
<!UNUSED_VALUE!>(label@ <!VAL_REASSIGNMENT!>j<!>) =<!> 34 //repeat for j
val a = A()
(l@ a.a) = 3894
}
fun canBe2(j: Int) {
(label@ <!VAL_REASSIGNMENT!>j<!>) = <!UNUSED_VALUE!>34<!>
<!UNUSED_VALUE!>(label@ <!VAL_REASSIGNMENT!>j<!>) =<!> 34
}
class A() {
@@ -2,14 +2,14 @@ package unused_variables
fun testSimpleCases() {
var i = <!VARIABLE_WITH_REDUNDANT_INITIALIZER!>2<!>
i = <!UNUSED_VALUE!>34<!>
<!UNUSED_VALUE!>i =<!> 34
i = 34
doSmth(i)
i = <!UNUSED_VALUE!>5<!>
<!UNUSED_VALUE!>i =<!> 5
var j = 2
j = <!UNUSED_CHANGED_VALUE!>j++<!>
j = <!UNUSED_VALUE, UNUSED_CHANGED_VALUE!>j--<!>
<!UNUSED_VALUE!>j =<!> <!UNUSED_CHANGED_VALUE!>j--<!>
}
class IncDec() {
@@ -27,14 +27,14 @@ class MyTest() {
x = <!UNUSED_CHANGED_VALUE!>x++<!>
x = <!UNUSED_CHANGED_VALUE!>x--<!>
x = ++x
x = <!UNUSED_VALUE!>--x<!>
<!UNUSED_VALUE!>x =<!> --x
}
var a: String = "s"
set(v: String) {
var i: Int = 23
doSmth(i)
i = <!UNUSED_VALUE!>34<!>
<!UNUSED_VALUE!>i =<!> 34
$a = v
}
@@ -46,8 +46,8 @@ class MyTest() {
a = "rro"
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>i<!> = 1;
i = <!UNUSED_VALUE!>34<!>;
i = <!UNUSED_VALUE!>456<!>;
<!UNUSED_VALUE!>i =<!> 34;
<!UNUSED_VALUE!>i =<!> 456;
}
fun testWhile() {
@@ -57,7 +57,7 @@ class MyTest() {
a = null
}
while (b != null) {
a = <!UNUSED_VALUE!>null<!>
<!UNUSED_VALUE!>a =<!> null
}
}
@@ -73,10 +73,10 @@ class MyTest() {
doSmth(a)
if (1 < 2) {
a = <!UNUSED_VALUE!>23<!>
<!UNUSED_VALUE!>a =<!> 23
}
else {
a = <!UNUSED_VALUE!>"ss"<!>
<!UNUSED_VALUE!>a =<!> "ss"
}
}
@@ -54,7 +54,7 @@ fun t2() {
class A() {}
fun t4(a: A) {
<!VAL_REASSIGNMENT!>a<!> = <!UNUSED_VALUE!>A()<!>
<!UNUSED_VALUE!><!VAL_REASSIGNMENT!>a<!> =<!> A()
}
// ------------------------------------------------
@@ -62,10 +62,10 @@ fun t4(a: A) {
fun t1() {
val <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>a<!> : Int = 1
<!VAL_REASSIGNMENT!>a<!> = <!UNUSED_VALUE!>2<!>
<!UNUSED_VALUE!><!VAL_REASSIGNMENT!>a<!> =<!> 2
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>b<!> : Int = 1
b = <!UNUSED_VALUE!>3<!>
<!UNUSED_VALUE!>b =<!> 3
}
enum class ProtocolState {
@@ -83,7 +83,7 @@ enum class ProtocolState {
fun t3() {
val x: ProtocolState = ProtocolState.WAITING
<!VAL_REASSIGNMENT!>x<!> = x.signal()
x = <!UNUSED_VALUE!>x.signal()<!> //repeat for x
<!UNUSED_VALUE!>x =<!> x.signal() //repeat for x
}
fun t4() {
@@ -189,7 +189,7 @@ class AnonymousInitializers(var a: String, val b: String) {
}
fun reassignFunParams(a: Int) {
<!VAL_REASSIGNMENT!>a<!> = <!UNUSED_VALUE!>1<!>
<!UNUSED_VALUE!><!VAL_REASSIGNMENT!>a<!> =<!> 1
}
open class Open(<!UNUSED_PARAMETER!>a<!>: Int, <!UNUSED_PARAMETER!>w<!>: Int) {}
@@ -4,7 +4,7 @@ package kt609
fun test(a: Int) {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>aa<!> = a
aa = <!UNUSED_VALUE!>324<!> //should be an 'unused value' warning here
<!UNUSED_VALUE!>aa =<!> 324 //should be an 'unused value' warning here
}
class C() {
@@ -6,5 +6,5 @@ fun foo() {
var <!UNUSED_VARIABLE!>j<!> = 9 //'unused variable' error
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>i<!> = 1 //should be an error 'variable i is assigned but never accessed'
i = <!UNUSED_VALUE!>2<!>
<!UNUSED_VALUE!>i =<!> 2
}
@@ -95,13 +95,13 @@ fun testImplicitCoercion() {
var <!UNUSED_VARIABLE!>u<!> = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>when(d) {
3 -> {
z = <!UNUSED_VALUE!>34<!>
<!UNUSED_VALUE!>z =<!> 34
}
else -> <!UNUSED_CHANGED_VALUE!>z--<!>
}<!>
var <!UNUSED_VARIABLE!>iff<!> = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) {
z = <!UNUSED_VALUE!>34<!>
<!UNUSED_VALUE!>z =<!> 34
}<!>
val <!UNUSED_VARIABLE!>g<!> = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) 4<!>
val <!UNUSED_VARIABLE!>h<!> = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (false) 4 else {}<!>
@@ -110,7 +110,7 @@ fun testImplicitCoercion() {
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>4<!>
}
else {
z = <!UNUSED_VALUE!>342<!>
<!UNUSED_VALUE!>z =<!> 342
})
}
@@ -118,9 +118,9 @@ fun bar(<!UNUSED_PARAMETER!>a<!>: Unit) {}
fun testStatementInExpressionContext() {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>z<!> = 34
val <!UNUSED_VARIABLE!>a1<!>: Unit = <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!>z = <!UNUSED_VALUE!>334<!><!>
val <!UNUSED_VARIABLE!>a1<!>: Unit = <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!><!UNUSED_VALUE!>z =<!> 334<!>
val <!UNUSED_VARIABLE!>f<!> = <!EXPRESSION_EXPECTED!>for (i in 1..10) {}<!>
if (true) return <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!>z = <!UNUSED_VALUE!>34<!><!>
if (true) return <!ASSIGNMENT_IN_EXPRESSION_CONTEXT!><!UNUSED_VALUE!>z =<!> 34<!>
return <!EXPRESSION_EXPECTED!>while (true) {}<!>
}
@@ -6,7 +6,7 @@ fun foo() : Int {
var z = 0
when(d) {
5, 3 -> <!UNUSED_CHANGED_VALUE!>z++<!>
<!ELSE_MISPLACED_IN_WHEN!>else<!> -> { z = <!UNUSED_VALUE!>-1000<!> }
<!ELSE_MISPLACED_IN_WHEN!>else<!> -> { <!UNUSED_VALUE!>z =<!> -1000 }
<!UNREACHABLE_CODE!>return z -> 34<!>
}
<!NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY!>}<!>
@@ -23,40 +23,40 @@ val x : Any? = 1
fun Any?.vars(<!UNUSED_PARAMETER!>a<!>: Any?) : Int {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>b<!>: Int = 0
if (ns.y is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>ns.y<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>ns.y<!>
}
if (ns.y is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>example.ns.y<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>example.ns.y<!>
}
if (example.ns.y is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>ns.y<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>ns.y<!>
}
if (example.ns.y is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>example.ns.y<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>example.ns.y<!>
}
// if (package.bottles.ns.y is Int) {
// b = ns.y
// }
if (Obj.y is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>Obj.y<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>Obj.y<!>
}
if (example.Obj.y is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>Obj.y<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>Obj.y<!>
}
if (AClass.y is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>AClass.y<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>AClass.y<!>
}
if (example.AClass.y is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>AClass.y<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>AClass.y<!>
}
if (x is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>x<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>x<!>
}
if (example.x is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>x<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>x<!>
}
if (example.x is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>example.x<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>example.x<!>
}
return 1
}
@@ -80,16 +80,16 @@ open class C {
fun foo() {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>t<!> : T? = null
if (this is T) {
t = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>this<!>
<!UNUSED_VALUE!>t =<!> <!DEBUG_INFO_SMARTCAST!>this<!>
}
if (this is T) {
t = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>this@C<!>
<!UNUSED_VALUE!>t =<!> <!DEBUG_INFO_SMARTCAST!>this@C<!>
}
if (this@C is T) {
t = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>this<!>
<!UNUSED_VALUE!>t =<!> <!DEBUG_INFO_SMARTCAST!>this<!>
}
if (this@C is T) {
t = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>this@C<!>
<!UNUSED_VALUE!>t =<!> <!DEBUG_INFO_SMARTCAST!>this@C<!>
}
}
}
@@ -9,7 +9,7 @@ class A {
fun foo(list: List<A>) {
for (var (c1, c2, c3) in list) {
c1 = <!UNUSED_VALUE!>1<!>
<!UNUSED_VALUE!>c1 =<!> 1
c3 + 1
}
}
@@ -17,7 +17,7 @@ fun test2(c: C) {
fun test3(c: C) {
var (<!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>a<!>, <!UNUSED_VARIABLE!>b<!>) = c
a = <!UNUSED_VALUE!>3<!>
<!UNUSED_VALUE!>a =<!> 3
}
fun test4(c: C) {
@@ -5,5 +5,5 @@ enum class E {
fun foo() {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>e<!> = E.E1
e = <!UNUSED_VALUE!>E.E2<!>
<!UNUSED_VALUE!>e =<!> E.E2
}
@@ -169,7 +169,7 @@ fun declarations(a: Any?) {
fun vars(a: Any?) {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>b<!>: Int = 0
if (a is Int) {
b = <!UNUSED_VALUE, DEBUG_INFO_SMARTCAST!>a<!>
<!UNUSED_VALUE!>b =<!> <!DEBUG_INFO_SMARTCAST!>a<!>
}
}
fun returnFunctionLiteralBlock(a: Any?): Function0<Int> {
@@ -14,7 +14,7 @@ fun foo(a: Any?): Int {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>b<!> = 1
(<!SYNTAX!>@<!> b) = <!UNUSED_VALUE!>2<!>
<!UNUSED_VALUE!>(<!SYNTAX!>@<!> b) =<!> 2
return<!SYNTAX!>@<!> 1
}
@@ -2,11 +2,11 @@ fun test() {
var res : Boolean = true
res = (res and false)
res = (res or false)
res = <!UNUSED_VALUE!>(res xor false)<!>
res = <!UNUSED_VALUE!>(true and false)<!>
res = <!UNUSED_VALUE!>(true or false)<!>
res = <!UNUSED_VALUE!>(true xor false)<!>
res = <!UNUSED_VALUE!>(!true)<!>
res = <!UNUSED_VALUE!>(true && false)<!>
res = <!UNUSED_VALUE!>(true || false)<!>
<!UNUSED_VALUE!>res =<!> (res xor false)
<!UNUSED_VALUE!>res =<!> (true and false)
<!UNUSED_VALUE!>res =<!> (true or false)
<!UNUSED_VALUE!>res =<!> (true xor false)
<!UNUSED_VALUE!>res =<!> (!true)
<!UNUSED_VALUE!>res =<!> (true && false)
<!UNUSED_VALUE!>res =<!> (true || false)
}
@@ -2,5 +2,5 @@ fun test() {
var a : Any? = null
if (a is Any) else a = null;
while (a is Any) a = null
while (true) a = <!UNUSED_VALUE!>null<!>
while (true) <!UNUSED_VALUE!>a =<!> null
}
@@ -13,7 +13,7 @@ fun main(args: Array<String>) {
}
val <!UNUSED_VARIABLE!>h<!>: () -> String = {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!> = 1
<!EXPECTED_TYPE_MISMATCH!>x = <!UNUSED_VALUE!>2<!><!> //the same
<!EXPECTED_TYPE_MISMATCH!><!UNUSED_VALUE!>x =<!> 2<!> //the same
}
val array1 = MyArray1()
val <!UNUSED_VARIABLE!>i<!>: () -> String = {