Mark only unreachable parts of element if it has reachable parts

like for 'return todo()' mark only 'return'
This commit is contained in:
Svetlana Isakova
2014-06-11 19:35:00 +04:00
parent 88ecc5cc59
commit 79cec6411d
38 changed files with 597 additions and 84 deletions
@@ -0,0 +1,12 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testCommasAndWhitespaces() {
fun bar(i: Int, s: String, x: Any) {}
<!UNREACHABLE_CODE!>bar(<!> 1 , todo() , <!UNREACHABLE_CODE!>"" )<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,11 @@
fun testInvoke() {
fun Nothing.invoke() = this
todo()<!UNREACHABLE_CODE!>()<!>
}
fun testInvokeWithLambda() {
fun Nothing.invoke(<!UNUSED_PARAMETER!>i<!>: Int, f: () -> Int) = f
todo()<!UNREACHABLE_CODE!>(1){ 42 }<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,11 @@
fun test11() {
fun Any.bar(<!UNUSED_PARAMETER!>i<!>: Int) {}
todo().<!UNREACHABLE_CODE!>bar(1)<!>
}
fun test12() {
fun Any.bar(<!UNUSED_PARAMETER!>i<!>: Int) {}
todo()<!UNNECESSARY_SAFE_CALL!>?.<!><!UNREACHABLE_CODE!>bar(1)<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,168 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
fun t1() : Int{
return 0
<!UNREACHABLE_CODE!>1<!>
}
fun t1a() : Int {
<!RETURN_TYPE_MISMATCH!>return<!>
<!UNREACHABLE_CODE!>return 1<!>
<!UNREACHABLE_CODE!>1<!>
}
fun t1b() : Int {
return 1
<!UNREACHABLE_CODE!>return 1<!>
<!UNREACHABLE_CODE!>1<!>
}
fun t1c() : Int {
return 1
<!RETURN_TYPE_MISMATCH, UNREACHABLE_CODE!>return<!>
<!UNREACHABLE_CODE!>1<!>
}
fun t2() : Int {
if (1 > 2)
return 1
else return 1
<!UNREACHABLE_CODE!>1<!>
}
fun t2a() : Int {
if (1 > 2) {
return 1
<!UNREACHABLE_CODE!>1<!>
} else { return 1
<!UNREACHABLE_CODE!>2<!>
}
<!UNREACHABLE_CODE!>1<!>
}
fun t3() : Any {
if (1 > 2)
return 2
else return ""
<!UNREACHABLE_CODE!>1<!>
}
fun t4(a : Boolean) : Int {
do {
return 1
}
while (<!UNREACHABLE_CODE!>a<!>)
<!UNREACHABLE_CODE!>1<!>
}
fun t4break(a : Boolean) : Int {
do {
break
}
while (<!UNREACHABLE_CODE!>a<!>)
return 1
}
fun t5() : Int {
do {
return 1
<!UNREACHABLE_CODE!>2<!>
}
while (<!UNREACHABLE_CODE!>1 > 2<!>)
<!UNREACHABLE_CODE!>return 1<!>
}
fun t6() : Int {
while (1 > 2) {
return 1
<!UNREACHABLE_CODE!>2<!>
}
return 1
}
fun t6break() : Int {
while (1 > 2) {
break
<!UNREACHABLE_CODE!>2<!>
}
return 1
}
fun t7(b : Int) : Int {
for (i in 1..b) {
return 1
<!UNREACHABLE_CODE!>2<!>
}
return 1
}
fun t7break(b : Int) : Int {
for (i in 1..b) {
return 1
<!UNREACHABLE_CODE!>2<!>
}
return 1
}
fun t7() : Int {
try {
return 1
<!UNREACHABLE_CODE!>2<!>
}
catch (<!TYPE_MISMATCH!>e : Any<!>) {
2
}
return 1 // this is OK, like in Java
}
fun t8() : Int {
try {
return 1
<!UNREACHABLE_CODE!>2<!>
}
catch (<!TYPE_MISMATCH!>e : Any<!>) {
return 1
<!UNREACHABLE_CODE!>2<!>
}
<!UNREACHABLE_CODE!>return 1<!>
}
fun blockAndAndMismatch() : Boolean {
(return true) <!UNREACHABLE_CODE!>|| (return false)<!>
<!UNREACHABLE_CODE!>return true<!>
}
fun tf() : Int {
try {<!UNREACHABLE_CODE!>return<!> 1} finally{return 1}
<!UNREACHABLE_CODE!>return 1<!>
}
fun failtest(a : Int) : Int {
if (fail() || <!UNREACHABLE_CODE!>true<!>) <!UNREACHABLE_CODE!>{
}<!>
<!UNREACHABLE_CODE!>return 1<!>
}
fun foo(a : Nothing) : Unit {
1
a
<!UNREACHABLE_CODE!>2<!>
}
fun fail() : Nothing {
throw java.lang.RuntimeException()
}
fun nullIsNotNothing() : Unit {
val x : Int? = 1
if (x != null) {
return
}
fail()
}
fun returnInWhile(a: Int) {
do {return}
while (<!UNREACHABLE_CODE!>1 > a<!>)
}
@@ -0,0 +1,23 @@
package c
fun test1() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>r<!>: Nothing =<!> null!!
}
fun test2(a: A) {
a + a
<!UNREACHABLE_CODE!>bar()<!>
}
fun test3() {
null!!
<!UNREACHABLE_CODE!>bar()<!>
}
fun throwNPE() = null!!
class A {
fun plus(<!UNUSED_PARAMETER!>a<!>: A): Nothing = throw Exception()
}
fun bar() {}
@@ -0,0 +1,40 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testArrayAccess1(array: Array<Any>) {
array<!UNREACHABLE_CODE!>[<!>todo()<!UNREACHABLE_CODE!>]<!>
}
fun testArrayAccess2() {
fun Nothing.get(i: Int, s: String) {}
todo()<!UNREACHABLE_CODE!>[1, ""]<!>
}
fun testAraryAccess3() {
fun Nothing.get(n: Nothing) {}
todo()<!UNREACHABLE_CODE!>[todo()]<!>
}
fun testArrayAssignment1(array: Array<Any>) {
array[todo()] <!UNREACHABLE_CODE!>= 11<!>
}
fun testArrayAssignment2(array: Array<Any>) {
array[1] <!UNREACHABLE_CODE!>=<!> todo()
}
fun testArrayAssignment3(n: Nothing) {
fun Nothing.set(i: Int, j: Int) {}
n<!UNREACHABLE_CODE!>[1] = 2<!>
}
fun testArrayAssignment4(n: Nothing) {
fun Nothing.set(i: Int, a: Any) {}
n<!UNREACHABLE_CODE!>[1] = todo()<!>
}
fun testArrayPlusAssign(array: Array<Any>) {
fun Any.plusAssign(a: Any) {}
array[1] <!UNREACHABLE_CODE!>+=<!> todo()
}
fun todo() = throw Exception()
@@ -0,0 +1,18 @@
fun testAssignment() {
var <!UNUSED_VARIABLE!>a<!> = 1
<!UNREACHABLE_CODE!>a =<!> todo()
}
fun testVariableDeclaration() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a<!> =<!> todo()
}
fun testPlusAssign() {
fun Int.plusAssign(<!UNUSED_PARAMETER!>i<!>: Int) {}
var a = 1
a <!UNREACHABLE_CODE!>+=<!> todo()
}
fun todo() = throw Exception()
@@ -0,0 +1,39 @@
fun testBinary1() {
fun Int.times(<!UNUSED_PARAMETER!>s<!>: String) {}
todo() <!UNREACHABLE_CODE!>* ""<!>
}
fun testBinary2() {
"1" <!UNREACHABLE_CODE!>+<!> todo()
}
fun testElvis1() {
<!USELESS_ELVIS!>todo()<!> <!UNREACHABLE_CODE!>?: ""<!>
}
fun testElvis2(s: String?) {
s ?: todo()
bar()
}
fun testAnd1(b: Boolean) {
b && todo()
bar()
}
fun testAnd2(<!UNUSED_PARAMETER!>b<!>: Boolean) {
todo() <!UNREACHABLE_CODE!>&& b<!>
}
fun returnInBinary1(): Boolean {
(return true) <!UNREACHABLE_CODE!>&& (return false)<!>
}
fun returnInBinary2(): Boolean {
(return true) <!UNREACHABLE_CODE!>|| (return false)<!>
}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testArgumentInCall() {
fun bar(i: Int, s: String, x: Any) {}
<!UNREACHABLE_CODE!>bar(<!>1, todo(), <!UNREACHABLE_CODE!>"")<!>
}
fun testArgumentInVariableAsFunctionCall(f: (Any) -> Unit) {
f<!UNREACHABLE_CODE!>(<!>todo()<!UNREACHABLE_CODE!>)<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,24 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun unreachable0() {
return
<!UNREACHABLE_CODE!>return todo()<!>
}
fun unreachable2() {
return
<!UNREACHABLE_CODE!>val a = todo()<!>
}
fun unreachable3() {
return
<!UNREACHABLE_CODE!>bar(todo())<!>
}
fun unreachable4(array: Array<Any>) {
return
<!UNREACHABLE_CODE!>array[todo()]<!>
}
fun bar(a: Any) {}
fun todo() = throw Exception()
@@ -0,0 +1,12 @@
fun testIf() {
if (todo()) <!UNREACHABLE_CODE!>1<!> else <!UNREACHABLE_CODE!>2<!>
}
fun testIf1(b: Boolean) {
if (b) todo() else 1
bar()
}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testCompound() {
fun Nothing.get(i: Int) {}
todo()<!UNREACHABLE_CODE!><!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>[12]<!>
}
fun testCompound1() {
fun Int.times(s: String): Array<String> = throw Exception()
<!UNREACHABLE_CODE!>(<!>todo() <!UNREACHABLE_CODE!>* "")[1]<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,36 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun testObject() {
<!UNREACHABLE_CODE!>object : Foo(<!>todo()<!UNREACHABLE_CODE!>) {
fun foo() = 1
}<!>
}
fun testObjectExpression() {
<!UNREACHABLE_CODE!>val <!UNUSED_VARIABLE!>a<!> = object : Foo(<!>todo()<!UNREACHABLE_CODE!>) {
fun foo() = 1
}<!>
}
fun testObjectExpression1() {
fun bar(i: Int, x: Any) {}
<!UNREACHABLE_CODE!>bar(<!>1, <!UNREACHABLE_CODE!>object : Foo(<!>todo()<!UNREACHABLE_CODE!>) {
fun foo() = 1
})<!>
}
fun testClassDeclaration() {
class C : Foo(todo()) {}
<!UNREACHABLE_CODE!>bar()<!>
}
fun testFunctionDefaultArgument() {
fun foo(x: Int = todo()) { bar() }
}
open class Foo(i: Int) {}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,21 @@
fun testFor() {
fun Nothing.iterator() = (0..1).iterator()
<!UNREACHABLE_CODE!>for (i in<!> todo()<!UNREACHABLE_CODE!>) {}<!>
}
fun testWhile() {
<!UNREACHABLE_CODE!>while (<!>todo()<!UNREACHABLE_CODE!>) {
}<!>
}
fun testDoWhile() {
do {
} while(todo())
<!UNREACHABLE_CODE!>bar()<!>
}
fun todo() = throw Exception()
fun bar() {}
@@ -0,0 +1,5 @@
fun testReturn() {
<!UNREACHABLE_CODE!>return<!> todo()
}
fun todo() = throw Exception()
@@ -0,0 +1,15 @@
fun testPrefix() {
fun Any.not() {}
<!UNREACHABLE_CODE!>!<!>todo()
}
fun testPostfixWithCall(n: Nothing) {
fun Nothing.inc(): Nothing = this
n<!UNREACHABLE_CODE!>++<!>
}
fun testPostfixSpecial() {
todo()<!UNNECESSARY_NOT_NULL_ASSERTION, UNREACHABLE_CODE!>!!<!>
}
fun todo() = throw Exception()
@@ -0,0 +1,55 @@
fun foo(<!UNUSED_PARAMETER!>a<!>: Any) {}
fun bar(<!UNUSED_PARAMETER!>a<!>: Any, <!UNUSED_PARAMETER!>b<!>: Any) {}
fun test(arr: Array<Int>) {
while (true) {
<!UNREACHABLE_CODE!>foo(<!>break<!UNREACHABLE_CODE!>)<!>
}
while (true) {
<!UNREACHABLE_CODE!>bar(<!>arr, break<!UNREACHABLE_CODE!>)<!>
}
while (true) {
arr<!UNREACHABLE_CODE!>[<!>break<!UNREACHABLE_CODE!>]<!>
}
while (true) {
arr[1] <!UNREACHABLE_CODE!>=<!> break
}
while (true) {
break
<!UNREACHABLE_CODE!>foo(1)<!>
}
while (true) {
var <!UNUSED_VARIABLE!>x<!> = 1
break
<!UNREACHABLE_CODE!>x = 2<!>
}
while (true) {
var <!UNUSED_VARIABLE!>x<!> = 1
<!UNREACHABLE_CODE!>x =<!> break
}
// TODO: bug, should be fixed in CFA
while (true) {
if (1 > 2 && break && 2 > 3) {
}
}
// TODO: bug, should be fixed in CFA
while (true) {
if (1 > 2 || break || 2 > 3) {
}
}
while (true) {
<!USELESS_ELVIS!>break<!> <!UNREACHABLE_CODE!>?: null<!>
}
}
@@ -0,0 +1,12 @@
//KT-2585 Code in try-finally is incorrectly marked as unreachable
fun foo(x: String): String {
try {
<!UNREACHABLE_CODE!>throw<!> RuntimeException()
} finally {
try {
} catch (e: Exception) {
}
return x // <- Wrong UNREACHABLE_CODE
}
}
@@ -0,0 +1,23 @@
//KT-2585 Code in try-finally is incorrectly marked as unreachable
fun foo() {
try {
<!UNREACHABLE_CODE!>throw<!> RuntimeException()
} catch (e: Exception) {
<!UNREACHABLE_CODE!>return<!> // <- Wrong UNREACHABLE_CODE
} finally {
while (true);
}
}
fun bar() {
try {
throw RuntimeException()
} catch (e: Exception) {
return // <- Wrong UNREACHABLE_CODE
} finally {
while (cond());
}
}
fun cond() = true
@@ -0,0 +1,9 @@
//KT-2585 Code in try-finally is incorrectly marked as unreachable
fun foo(<!UNUSED_PARAMETER!>x<!>: String): String {
try {
<!UNREACHABLE_CODE!>throw<!> RuntimeException() //should be marked as unreachable, but is not
} finally {
throw NullPointerException()
}
}