Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin suppress warnings for Receiver parameter is never used

So, I have one file where I added extension on some class. Something like

val A.extension
    get = 1+1

And I get warning Receiver parameter is never used that I want to suppress. I tried multiple wild guesses like @file:Suppress("UNUSED_PARAMETER") but nothing worked.

Does someone know which string I am looking for? Second part of the question: Is there full list of suppress warnings? Usually there are only hardcoded strings, that I find all over internet, but not list suppress key - suppress description.

EDIT: I know suppress "unused" would do the work, but it is too generic.

EDIT2: I want to suppress specific warning in the whole file, I think it "unused" too generic in way that if I have A.extension and extension is never used, I want it gray(Property unused), but I do not want to have A gray (Receiver parameter unused).

like image 720
Милош Којадиновић Avatar asked Sep 19 '25 16:09

Милош Којадиновић


2 Answers

The answer given above about using @receiver:Suppress("Unused") was indeed the best solution until the recent Android Studio update.

I found out that @Suppress("UnusedReceiverParameter") can be used to achieve the same effect (i.e. suppressing the receiver parameter usage without affecting the unused status of the function itself). Personally, I think this change is for the better as it doesn't interfere with the function signature declaration.

like image 76
V Mircan Avatar answered Sep 23 '25 12:09

V Mircan


So, I found it finally. It is possible to suppress things just for receiver with adding annotation @receiver:Suppress("Unused")

val @receiver:Suppress("Unused") A.extension
    get = 1+1

And it will still get grayed out if extension is not used.

Edit: I aded my response before V Mircan answer, but I think his annotation is better.

like image 29
Милош Којадиновић Avatar answered Sep 23 '25 12:09

Милош Којадиновић