如何抑制未经检查的强制转换警告? [英] How can I suppress unchecked cast warnings?

查看:37
本文介绍了如何抑制未经检查的强制转换警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下代码:

fun doSomething(): List<String> {

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

    return test as List<String>
}

有没有办法抑制最后一行出现的未经检查的强制转换警告?我尝试在方法级别使用标准的 Java 方式 @SuppressWarnings("unchecked"),但没有奏效.

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.

推荐答案

Adding @Suppress("UNCHECKED_CAST")(也可以通过 IDEA 的 Alt+Enter 菜单)到 statementfunctionclassfile 中的任何一个都应该有帮助.

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

之前:

之后:

这篇关于如何抑制未经检查的强制转换警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆