tomrozb
tomrozb

Reputation: 26271

How to suppress warnings for a single line in Kotlin

I have Android app written in Kotlin. There's a warning in my code and I want to suppress it for a single line, not the entire function.

Is is possible in Kotlin? Something like that

fun largeFun(canvas: Canvas) {
    ...

    if (BuildConfig.DEBUG) {
        // Suppress allocation warning
        canvas.drawText(Date().toString, x, y, p)
    }
}

PS I know that I can extract that code to a separate function and suppress the warning for that function, but still I want to know if single line warning suppressing is possible

EDIT Edited the question code. The solution is to extract allocation to val and @Suppress works as expected

Upvotes: 2

Views: 3873

Answers (1)

Cililing
Cililing

Reputation: 4763

Just put annotation Suppress right before the scope you want to suppress warnings. For example:

fun main() {
    @Deprecated(level = WARNING)
    fun deprecatedFun() {}

    if (BuildConfig.DEBUG) {
        @Suppress("DEPRECATION") val f = deprecatedFun()
    }
}

Also, IntelliJ-based IDEs gives a nice possibility to suppress the warning for specific scope - alt + enter ;)

enter image description here

Upvotes: 10

Related Questions