KT-2223 Comparing non-null value with null might produce helpful warning

#KT-2223 Fixed
This commit is contained in:
Alexander Udalov
2012-06-20 17:20:45 +04:00
parent aed160e02a
commit d67e22b174
12 changed files with 92 additions and 46 deletions
@@ -1,4 +1,3 @@
fun test() {
val a : Int? = 0
if (a != null) {
@@ -53,7 +52,7 @@ fun test() {
out?.println();
}
if (out == null || out != null && out.println() == #()) {
if (out == null || out.println() == #()) {
out?.println();
}
else {
@@ -109,7 +108,7 @@ fun test() {
out?.println();
}
if (out == null || out != null && out.println() == #()) {
if (out == null || out.println() == #()) {
out?.println();
}
else {
@@ -134,10 +133,12 @@ fun test() {
}
out?.println();
while (out == null) {
out?.println();
val out2 : java.io.PrintStream? = null
while (out2 == null) {
out2?.println();
}
out.println()
out2.println()
}
@@ -247,7 +248,7 @@ fun f7(s : String?, t : String?) {
}
s?.get(0)
t?.get(0)
if (!(s == null && s == null)) {
if (!(s == null)) {
s.get(0)
t?.get(0)
}
@@ -5,24 +5,24 @@ fun foo() {
if (x != null) {
bar(x)
if (x != null) {
if (<!SENSELESS_COMPARISON!>x != null<!>) {
bar(x)
if (1 < 2) bar(x)
if (1 > 2) bar(x)
}
if (x == null) {
if (<!SENSELESS_COMPARISON!>x == null<!>) {
bar(x)
}
if (x == null) bar(x) else bar(x)
if (<!SENSELESS_COMPARISON!>x == null<!>) bar(x) else bar(x)
bar(bar(x))
} else if (x == null) {
} else if (<!SENSELESS_COMPARISON!>x == null<!>) {
bar(<!TYPE_MISMATCH!>x<!>)
if (x != null) {
if (<!SENSELESS_COMPARISON!>x != null<!>) {
bar(x)
if (x == null) bar(x)
if (x == null) bar(x) else bar(x)
bar(bar(x) + bar(x))
} else if (x == null) {
} else if (<!SENSELESS_COMPARISON!>x == null<!>) {
bar(<!TYPE_MISMATCH!>x<!>)
}
}
@@ -1,13 +1,23 @@
fun bar(x: Int) = x + 1
fun foo() {
val x: Int? = null
fun f1(x: Int?) {
bar(<!TYPE_MISMATCH!>x<!>)
if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
if (x == null) bar(x!!)
if (x != null) else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
if (x == null) else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>) else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
if (x == null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>) else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
}
fun f2(x: Int?) {
if (x != null) else bar(x!!)
}
fun f3(x: Int?) {
if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>) else bar(x!!)
}
fun f4(x: Int?) {
if (x == null) bar(x!!) else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
}
fun f5(x: Int?) {
if (x == null) else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
}
@@ -12,7 +12,7 @@ fun foo() {
bar(x)
for (q in a) {
bar(x)
if (x == null) bar(x)
if (<!SENSELESS_COMPARISON!>x == null<!>) bar(x)
}
}
@@ -26,5 +26,5 @@ fun foo() {
if (z != null) bar(z)
bar(<!TYPE_MISMATCH!>z<!>)
bar(z!!)
if (z != null) bar(z<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
if (<!SENSELESS_COMPARISON!>z != null<!>) bar(z<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
}
@@ -9,7 +9,7 @@ fun foo() {
2+<!SYNTAX!><!>
}
else {
if (x == null) return
if (<!SENSELESS_COMPARISON!>x == null<!>) return
2+<!SYNTAX!><!>
}
bar(x)
@@ -5,8 +5,12 @@ fun foo(): Int {
bar(<!TYPE_MISMATCH!>x<!>)
if (x != null) return x
if (x == null) return if (x != null) x else <!TYPE_MISMATCH!>x<!>
if (x == null) return x
if (x != null) return if (x == null) x else x
return x
val y: Int? = null
if (y == null) return if (<!SENSELESS_COMPARISON!>y != null<!>) y else <!TYPE_MISMATCH!>y<!>
val z: Int? = null
if (z != null) return if (<!SENSELESS_COMPARISON!>z == null<!>) z else z
return <!TYPE_MISMATCH!>z<!>
}
@@ -3,8 +3,7 @@ fun bar(x: Int): RuntimeException = RuntimeException(x.toString())
fun foo() {
val x: Int? = null
if (x == null || 1 < 2) throw bar(<!TYPE_MISMATCH!>x<!>)
if (x == null) return
if (x == null) throw bar(<!TYPE_MISMATCH!>x<!>)
throw bar(x)
throw <!UNREACHABLE_CODE!>bar(x)<!>
}
@@ -0,0 +1,8 @@
//KT-2223 Comparing non-null value with null might produce helpful warning
package kt2223
fun foo() {
val x: Int? = null
if (x == null) return
if (<!SENSELESS_COMPARISON!>x == null<!>) return
}