FIR checker: warn useless elvis
This commit is contained in:
committed by
TeamCityServer
parent
b2005302dc
commit
24d792fb49
-23
@@ -1,23 +0,0 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER, -UNUSED_VARIABLE
|
||||
|
||||
fun baz(i: Int) = i
|
||||
fun <T> bar(x: T): T = TODO()
|
||||
|
||||
fun nullableFun(): ((Int) -> Int)? = null
|
||||
|
||||
fun test() {
|
||||
val x1: (Int) -> Int = bar(if (true) ::baz else ::baz)
|
||||
val x2: (Int) -> Int = bar(nullableFun() ?: ::baz)
|
||||
val x3: (Int) -> Int = bar(::baz ?: ::baz)
|
||||
|
||||
val i = 0
|
||||
val x4: (Int) -> Int = bar(when (i) {
|
||||
10 -> ::baz
|
||||
20 -> ::baz
|
||||
else -> ::baz
|
||||
})
|
||||
|
||||
val x5: (Int) -> Int = bar(::baz<!NOT_NULL_ASSERTION_ON_CALLABLE_REFERENCE!>!!<!>)
|
||||
|
||||
(if (true) ::baz else ::baz)(1)
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
// FIR_IDENTICAL
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER, -UNUSED_VARIABLE
|
||||
|
||||
fun baz(i: Int) = i
|
||||
|
||||
+2
-2
@@ -24,8 +24,8 @@ fun main() {
|
||||
val x17 = <!UNRESOLVED_REFERENCE!>logger<!>::info<!NOT_NULL_ASSERTION_ON_CALLABLE_REFERENCE!>!!<!>?::<!UNRESOLVED_REFERENCE!>print<!>?::<!UNRESOLVED_REFERENCE!>print<!>
|
||||
|
||||
// It must be OK
|
||||
val x18 = String?::hashCode ?: ::foo
|
||||
val x19 = String::hashCode ?: ::foo
|
||||
val x18 = String?::hashCode <!USELESS_ELVIS!>?: ::foo<!>
|
||||
val x19 = String::hashCode <!USELESS_ELVIS!>?: ::foo<!>
|
||||
val x20 = String?::hashCode::hashCode
|
||||
val x21 = kotlin.String?::hashCode::hashCode
|
||||
}
|
||||
|
||||
Vendored
+2
-2
@@ -8,7 +8,7 @@ fun testBinary2() {
|
||||
}
|
||||
|
||||
fun testElvis1() {
|
||||
todo() ?: ""
|
||||
todo() <!USELESS_ELVIS!>?: ""<!>
|
||||
}
|
||||
|
||||
fun testElvis2(s: String?) {
|
||||
@@ -36,4 +36,4 @@ fun returnInBinary2(): Boolean {
|
||||
}
|
||||
|
||||
fun todo(): Nothing = throw Exception()
|
||||
fun bar() {}
|
||||
fun bar() {}
|
||||
|
||||
Vendored
+1
-1
@@ -50,6 +50,6 @@ fun test(arr: Array<Int>) {
|
||||
}
|
||||
|
||||
while (true) {
|
||||
break ?: null
|
||||
break <!USELESS_ELVIS!>?: null<!>
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -2,20 +2,20 @@
|
||||
// See KT-8277
|
||||
// NI_EXPECTED_FILE
|
||||
|
||||
val v = { true } ?: ( { true } ?:null!! )
|
||||
val v = { true } <!USELESS_ELVIS!>?: ( { true } <!USELESS_ELVIS!>?:null!!<!> )<!>
|
||||
|
||||
val w = if (true) {
|
||||
{ true }
|
||||
}
|
||||
else {
|
||||
{ true } ?: null!!
|
||||
{ true } <!USELESS_ELVIS!>?: null!!<!>
|
||||
}
|
||||
|
||||
val ww = if (true) {
|
||||
{ true } ?: null!!
|
||||
{ true } <!USELESS_ELVIS!>?: null!!<!>
|
||||
}
|
||||
else if (true) {
|
||||
{ true } ?: null!!
|
||||
{ true } <!USELESS_ELVIS!>?: null!!<!>
|
||||
}
|
||||
else {
|
||||
null!!
|
||||
@@ -29,11 +29,11 @@ val b = null ?: ( l() ?: false)
|
||||
|
||||
val bb = null ?: ( l() ?: null!!)
|
||||
|
||||
val bbb = null ?: ( l() ?: null)
|
||||
val bbb = null ?: ( l() <!USELESS_ELVIS_RIGHT_IS_NULL!>?: null<!>)
|
||||
|
||||
val bbbb = ( l() ?: null) ?: ( l() ?: null)
|
||||
val bbbb = ( l() <!USELESS_ELVIS_RIGHT_IS_NULL!>?: null<!>) ?: ( l() <!USELESS_ELVIS_RIGHT_IS_NULL!>?: null<!>)
|
||||
|
||||
fun f(x : Long?): Long {
|
||||
var a = x ?: (fun() {} ?: fun() {})
|
||||
var a = x ?: (fun() {} <!USELESS_ELVIS!>?: fun() {}<!>)
|
||||
return <!RETURN_TYPE_MISMATCH!>a<!>
|
||||
}
|
||||
|
||||
+1
-1
@@ -7,6 +7,6 @@ fun foo(): String {
|
||||
}
|
||||
fun bar(): String {
|
||||
val x = fn() ?: return ""
|
||||
val y = x<!UNNECESSARY_SAFE_CALL!>?.<!>let { throw Exception() } ?: "unreachable"
|
||||
val y = x<!UNNECESSARY_SAFE_CALL!>?.<!>let { throw Exception() } <!USELESS_ELVIS!>?: "unreachable"<!>
|
||||
return y
|
||||
}
|
||||
|
||||
+4
-4
@@ -8,9 +8,9 @@ fun test() {
|
||||
use({ }<!NOT_NULL_ASSERTION_ON_LAMBDA_EXPRESSION!>!!<!>);
|
||||
|
||||
// KT-KT-9070
|
||||
{ } ?: 1
|
||||
use({ 2 } ?: 1);
|
||||
{ } <!USELESS_ELVIS!>?: 1<!>
|
||||
use({ 2 } <!USELESS_ELVIS!>?: 1<!>);
|
||||
|
||||
1 ?: { }
|
||||
use(1 ?: { })
|
||||
1 <!USELESS_ELVIS!>?: { }<!>
|
||||
use(1 <!USELESS_ELVIS!>?: { }<!>)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ fun foo() {
|
||||
val x: Int? = null
|
||||
|
||||
bar(x ?: 0)
|
||||
if (x != null) bar(x ?: x)
|
||||
if (x != null) bar(x <!USELESS_ELVIS!>?: x<!>)
|
||||
bar(<!ARGUMENT_TYPE_MISMATCH!>x<!>)
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -4,7 +4,7 @@
|
||||
fun <D> makeDefinitelyNotNull(arg: D?): D = TODO()
|
||||
|
||||
fun <N : Number?> test(arg: N) {
|
||||
makeDefinitelyNotNull(arg) ?: 1
|
||||
makeDefinitelyNotNull(arg) <!USELESS_ELVIS!>?: 1<!>
|
||||
|
||||
makeDefinitelyNotNull(arg)<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
|
||||
|
||||
|
||||
@@ -5,12 +5,12 @@ fun <T: Any?> test1(t: Any?): Any {
|
||||
}
|
||||
|
||||
fun <T: Any> test2(t: Any?): Any {
|
||||
return t as T ?: ""
|
||||
return t as T <!USELESS_ELVIS!>?: ""<!>
|
||||
}
|
||||
|
||||
fun <T: Any?> test3(t: Any?): Any {
|
||||
if (t != null) {
|
||||
return t ?: ""
|
||||
return t <!USELESS_ELVIS!>?: ""<!>
|
||||
}
|
||||
|
||||
return 1
|
||||
@@ -28,11 +28,11 @@ fun test() {
|
||||
val x: String? = null
|
||||
takeNotNull(dependOn(x) ?: "")
|
||||
takeNotNull(dependOn(dependOn(x)) ?: "")
|
||||
takeNotNull(dependOn(dependOn(x as String)) ?: "")
|
||||
takeNotNull(dependOn(dependOn(x as String)) <!USELESS_ELVIS!>?: ""<!>)
|
||||
|
||||
if (x != null) {
|
||||
takeNotNull(dependOn(x) ?: "")
|
||||
takeNotNull(dependOn(dependOn(x)) ?: "")
|
||||
takeNotNull(dependOn(x) <!USELESS_ELVIS!>?: ""<!>)
|
||||
takeNotNull(dependOn(dependOn(x)) <!USELESS_ELVIS!>?: ""<!>)
|
||||
takeNotNull(dependOn(dependOn(x) as? String) ?: "")
|
||||
}
|
||||
|
||||
@@ -45,4 +45,4 @@ fun testFrom13648() {
|
||||
takeNotNull(reifiedNull() ?: "")
|
||||
}
|
||||
|
||||
fun bar() = <!UNRESOLVED_REFERENCE!>unresolved<!>
|
||||
fun bar() = <!UNRESOLVED_REFERENCE!>unresolved<!>
|
||||
|
||||
+7
-7
@@ -36,28 +36,28 @@ fun test() {
|
||||
// platform type with no annotation
|
||||
val platformJ = J.staticJ
|
||||
|
||||
val v0 = platformNN ?: J()
|
||||
platformNN ?: J()
|
||||
val v0 = platformNN <!USELESS_ELVIS!>?: J()<!>
|
||||
platformNN <!USELESS_ELVIS!>?: J()<!>
|
||||
platformN ?: J()
|
||||
platformJ ?: J()
|
||||
|
||||
if (platformNN != null) {
|
||||
platformNN ?: J()
|
||||
platformNN <!USELESS_ELVIS!>?: J()<!>
|
||||
}
|
||||
|
||||
if (platformN != null) {
|
||||
platformN ?: J()
|
||||
platformN <!USELESS_ELVIS!>?: J()<!>
|
||||
}
|
||||
|
||||
if (platformJ != null) {
|
||||
platformJ ?: J()
|
||||
platformJ <!USELESS_ELVIS!>?: J()<!>
|
||||
}
|
||||
|
||||
takeNotNull(J.staticNN ?: J())
|
||||
takeNotNull(J.staticNN <!USELESS_ELVIS!>?: J()<!>)
|
||||
takeNotNull(J.staticN ?: J())
|
||||
takeNotNull(J.staticJ ?: J())
|
||||
takeNotNull(J.getAny() ?: J())
|
||||
takeNotNull(J.getNNAny() ?: J())
|
||||
takeNotNull(J.getNNAny() <!USELESS_ELVIS!>?: J()<!>)
|
||||
takeNotNull(J.getNAny() ?: J())
|
||||
|
||||
val x = <!UNRESOLVED_REFERENCE!>unresolved<!> ?: null
|
||||
|
||||
Vendored
-28
@@ -1,28 +0,0 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
// FILE: J.java
|
||||
|
||||
import org.jetbrains.annotations.*;
|
||||
|
||||
public class J {
|
||||
@NotNull
|
||||
public static J staticNN;
|
||||
}
|
||||
|
||||
// FILE: k.kt
|
||||
|
||||
fun test() {
|
||||
// @NotNull platform type
|
||||
val platformNN = J.staticNN
|
||||
|
||||
foo(platformNN ?: "")
|
||||
|
||||
val bar = Bar()
|
||||
bar(platformNN ?: "")
|
||||
}
|
||||
|
||||
fun foo(a: Any) {}
|
||||
|
||||
class Bar {
|
||||
operator fun invoke(a: Any) {}
|
||||
}
|
||||
Vendored
+1
@@ -1,3 +1,4 @@
|
||||
// FIR_IDENTICAL
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
// FILE: J.java
|
||||
|
||||
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/uselessElvisRightIsNull.fir.kt
Vendored
+4
-4
@@ -33,15 +33,15 @@ public interface JJJJ<R> {
|
||||
// FILE: k.kt
|
||||
|
||||
fun test() {
|
||||
val a = J.staticN ?: null
|
||||
val a = J.staticN <!USELESS_ELVIS_RIGHT_IS_NULL!>?: null<!>
|
||||
foo(a)
|
||||
val b = JJ.staticNN ?: null
|
||||
val b = JJ.staticNN <!USELESS_ELVIS_RIGHT_IS_NULL!>?: null<!>
|
||||
foo(b)
|
||||
val c = JJJ.staticNNN ?: null
|
||||
val c = JJJ.staticNNN <!USELESS_ELVIS!>?: null<!>
|
||||
foo(c)
|
||||
}
|
||||
|
||||
fun foo(a: Any?) {
|
||||
}
|
||||
|
||||
fun <R> test2(j: JJJJ<R>) = j.get() ?: null
|
||||
fun <R> test2(j: JJJJ<R>) = j.get() <!USELESS_ELVIS_RIGHT_IS_NULL!>?: null<!>
|
||||
|
||||
+1
-1
@@ -6,5 +6,5 @@ operator fun String.invoke(i: Int) {}
|
||||
fun foo(s: String?) {
|
||||
<!UNSAFE_IMPLICIT_INVOKE_CALL!>s<!>(1)
|
||||
|
||||
<!UNSAFE_IMPLICIT_INVOKE_CALL!>(s ?: null)<!>(1)
|
||||
<!UNSAFE_IMPLICIT_INVOKE_CALL!>(s <!USELESS_ELVIS_RIGHT_IS_NULL!>?: null<!>)<!>(1)
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ fun baz(s: String?, r: String?): String {
|
||||
}
|
||||
|
||||
fun withNull(s: String?): String {
|
||||
val t = s ?: null
|
||||
val t = s <!USELESS_ELVIS_RIGHT_IS_NULL!>?: null<!>
|
||||
// Error: nullable
|
||||
return <!RETURN_TYPE_MISMATCH!>t<!>
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -62,5 +62,5 @@ fun k() {
|
||||
fun invokeTest(goodArgs: Array<String>) {
|
||||
J.staticFun(*goodArgs)
|
||||
J.staticFun(*args)
|
||||
J.staticFun(*args ?: null)
|
||||
J.staticFun(*args <!USELESS_ELVIS_RIGHT_IS_NULL!>?: null<!>)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user