130

I am defining some functions to be used as callbacks and not all of them use all their parameters.

How can I mark unused parameters so that the compiler won't give me warnings about them?

TheTeaMan
  • 2,373
  • 3
  • 15
  • 13

4 Answers4

203

With the @Suppress annotation You can suppress any diagnostics on any declaration or expression.

Examples: Suppress warning on parameter:

fun foo(a: Int, @Suppress("UNUSED_PARAMETER") b: Int) = a

Suppress all UNUSED_PARAMETER warnings inside declaration

@Suppress("UNUSED_PARAMETER")
fun foo(a: Int,  b: Int) {
  fun bar(c: Int) {}
}

@Suppress("UNUSED_PARAMETER")
class Baz {
    fun foo(a: Int,  b: Int) {
        fun bar(c: Int) {}
    }
}

Additionally IDEA's intentions(Alt+Enter) can help you to suppress any diagnostics:

aaaidan
  • 7,093
  • 8
  • 66
  • 102
bashor
  • 8,073
  • 4
  • 34
  • 33
  • 1
    thanks. is there a way to shorten it to [unused] or something? I tried `annotation class unused : suppress("UNUSED_PARAMETER")` but it doesn't work due to suppress being final. – TheTeaMan Mar 14 '15 at 10:06
  • You should be able to omit the brackets, that saves two characters :) – Kirill Rakhman Mar 14 '15 at 13:37
  • Additionally You can extract and share annotation parameter. – bashor Mar 14 '15 at 13:52
  • My comment applied to the old, now removed annotation syntax and is no longer valid. – Kirill Rakhman Mar 17 '16 at 12:35
  • 1
    I had listenener methods with multiple arguments and only one was used. I was able to set ```@Suppress("UNUSED_PARAMETER")``` above the method. So all three unused arguments of four in total are covered :) – ecth Sep 03 '19 at 09:25
9

If your parameter is in a lambda, you can use an underscore to omit it. This removes the unused parameter warnings. It will also prevent IllegalArgumentException in the case that the parameter was null and was marked non-null.

See https://kotlinlang.org/docs/reference/lambdas.html#underscore-for-unused-variables-since-11

Mike
  • 10,297
  • 2
  • 21
  • 21
0

If the functions are part of a class you can declare the containing class open or abstract and the offending methods as open.

open class ClassForCallbacks {
  // no warnings here!
  open fun methodToBeOverriden(a: Int, b: Boolean) {}
}

Or

abstract class ClassForCallbacks {
  // no warnings here!
  open fun methodToBeOverriden(a: Int, b: Boolean) {}
}
Justin Fiedler
  • 6,478
  • 3
  • 21
  • 25
-9

One can disable these warnings by adding a kotlin compile option flag in build.gradle. To configure a single task, use its name. Examples:

compileKotlin {
    kotlinOptions.suppressWarnings = true
}

compileKotlin {
    kotlinOptions {
        suppressWarnings = true
    }
}

It is also possible to configure all Kotlin compilation tasks in the project:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        // ...
    }
}

If one is using kotlin in Android and want to suppress kotlin compiler warnings, add below in app-module build.gradle file

android{
    ....other configurations
    kotlinOptions {
        suppressWarnings = true
    }
}

Whether you really need to suppress all kotlin warning for your project or not, its up to you.

MatPag
  • 41,742
  • 14
  • 105
  • 114
Mahendra Chhimwal
  • 1,810
  • 5
  • 21
  • 33
  • 38
    Why would someone suppress the warning for the entire project?? It is not a good practice. – Xenolion Aug 23 '18 at 19:22
  • @"Mahendra Chhimwal" Thank you! Finally I can disable Jetbrain's annoying style warnings globally. Now I can code personal projects without Jetbrain's unwanted, unneeded, invasive style warning harassment. If not for this fix, I'd switch to VSCode. – devdanke Apr 19 '22 at 17:33
  • 2
    @devdanke "Most people don't have that willingness to break bad habits. They have a lot of excuses and they talk like victims." -Carlos Santana :) – simpleuser Aug 10 '22 at 17:16