Introduce "Redundant else in if" inspection #KT-19668 Fixed

This commit is contained in:
Mikhail Glukhikh
2018-11-29 15:23:59 +03:00
parent ca87e53f04
commit 7cbc8e8b76
24 changed files with 430 additions and 0 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.RedundantElseInIfInspection
@@ -0,0 +1,14 @@
// PROBLEM: none
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Boolean, y: Boolean) {
if (x) {
throw SomeException()
} else if (y) {
// empty
} else<caret> {
foo()
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
fun foo() {}
fun bar() {}
fun test(s: String?, b: Boolean) {
s?.also {
if (b) {
foo()
return
} <caret>else {
bar()
return
}
}
}
@@ -0,0 +1,14 @@
// WITH_RUNTIME
fun foo() {}
fun bar() {}
fun test(s: String?, b: Boolean) {
s?.also {
if (b) {
foo()
return
}
bar()
return
}
}
@@ -0,0 +1,11 @@
// PROBLEM: none
// WITH_RUNTIME
class SomeException : RuntimeException()
fun test(x: Boolean, y: Boolean) {
if (x) {
throw SomeException()
} else<caret> if (y) {
return
}
}
@@ -0,0 +1,9 @@
// PROBLEM: none
fun foo(): Int = 1
fun bar(): Int = 2
fun test(x: Boolean, y: Boolean) {
if (x) foo()
else if (y) return
else<caret> bar()
}
@@ -0,0 +1,16 @@
// PROBLEM: none
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun bar(): Int = 2
fun test(x: Boolean, y: Boolean) {
if (x) {
foo()
} else if (y) {
throw SomeException()
} else<caret> {
foo()
bar()
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Boolean) {
if (x) throw SomeException()
else<caret> foo()
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Boolean) {
if (x) throw SomeException()
foo()
}
@@ -0,0 +1,11 @@
fun foo(): Int = 1
fun bar(): Int = 2
fun test(x: Boolean) {
if (x) {
return
} else<caret> {
foo()
bar()
}
}
@@ -0,0 +1,10 @@
fun foo(): Int = 1
fun bar(): Int = 2
fun test(x: Boolean) {
if (x) {
return
}
foo()
bar()
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Boolean, y: Boolean) {
if (x) throw SomeException()
else if (y) return
else<caret> foo()
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Boolean, y: Boolean) {
if (x) throw SomeException()
else if (y) return
foo()
}
@@ -0,0 +1,17 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun bar(): Int = 2
fun test(x: Boolean, y: Boolean) {
if (x) {
return
} else if (y) {
throw SomeException()
} else<caret> {
// comment1
foo()
// comment2
bar()
}
}
@@ -0,0 +1,16 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun bar(): Int = 2
fun test(x: Boolean, y: Boolean) {
if (x) {
return
} else if (y) {
throw SomeException()
}
// comment1
foo()
// comment2
bar()
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Boolean): Int {
if (x) throw SomeException() else<caret> return foo()
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Boolean): Int {
if (x) throw SomeException()
return foo()
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Int): Int {
if (x == 1) {
throw SomeException()
} else if (x == 2) {
throw SomeException()
} <caret>else return foo()
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Int): Int {
if (x == 1) {
throw SomeException()
} else if (x == 2) {
throw SomeException()
}
return foo()
}
@@ -0,0 +1,10 @@
// PROBLEM: none
// WITH_RUNTIME
class SomeException : RuntimeException()
fun foo(): Int = 1
fun test(x: Boolean, y: Boolean) {
val i: Int = if (x) throw SomeException()
else if (y) return
else<caret> foo()
}