Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress unchecked cast warnings?

Tags:

kotlin

Having the following code:

fun doSomething(): List<String> {

    val test: List<*> = arrayListOf("test1", "test2")

    return test as List<String>
}

Is there some way to suppress the unchecked cast warning that comes up in the last line? I tried to use the standard Java way @SuppressWarnings("unchecked") at the method level, but it didn't work.

like image 450
mhlz Avatar asked Mar 25 '16 14:03

mhlz


People also ask

What is suppress warning unchecked?

@SuppressWarnings("unchecked") is used when Java generics just don't let you do what you want to, and thus, you need to explicitly specify to the compiler that whatever you are doing is legal and can be executed at the time of execution.

How do I get rid of kotlin unchecked cast warning?

1 Answer. Show activity on this post. Adding @Suppress("UNCHECKED_CAST") (also possible through IDEA's Alt + Enter menu) to any of statement, function, class and file should help.

What is unchecked cast?

Unchecked cast means that you are (implicitly or explicitly) casting from a generic type to a nonqualified type or the other way around.


1 Answers

Adding @Suppress("UNCHECKED_CAST") (also possible through IDEA's Alt+Enter menu) to any of statement, function, class and file should help.

Before:

enter image description here

After:

enter image description here

like image 156
hotkey Avatar answered Oct 19 '22 20:10

hotkey