具有以下代码:

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.

推荐答案

statementfunctionclassfile中的任意一个添加@Suppress("UNCHECKED_CAST")(也可以通过IDEA的Alt+Enter菜单)应该会有所帮助.

之前:

enter image description here

之后:

enter image description here

Kotlin相关问答推荐

Lambda和普通Kotlin函数有什么区别?

如何从 Period.between() 返回的字符串中提取信息? (Kotlin )

使用 Jetpack Compose 使用参数导航

通用接口继承

Eclipse:无法安装 Kotlin 插件

Kotlin 条件格式字符串

如何使用 Android CameraX 自动对焦

如何有效地填充 Gradle Kotlin DSL 中的额外属性?

@InlineOnly 注释是什么意思?

将 Gradle 子元素与 Kotlin 多平台一起使用

`this@classname` 在 Kotlin 中是什么意思?

为什么我在使用 Jetpack Compose clickable-Modifier 时收到后端内部错误:Exception during IR lowering error?

如何在 Kotlin 文件中的 Android Studio 中控制何时将 Imports 替换为通配符

Kotlin 类的调用方法

在 Koin 中提供一个 Instance 作为其接口

Mockito 的 argThat 在 Kotlin 中返回 null

大小写敏感性 Kotlin / ignoreCase

修改扩展函数中的this

Lint 错误:可疑的相等判断:在 Object DiffUtilEquals 中未实现 equals()

在Kotlin中将列表转换为对的惯用方法