androidkotlinsuppress-warnings

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


Solution

  • 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