https://kotlinlang.org logo
#detekt
Title
c

Charles

05/21/2020, 3:20 PM
Is there a good way to disallow a certain annotation whole-cloth? I am trying to not let people use Java's
@SuppressWarnings
in Kotlin code, because we're trying to track warning suppressing and normalizing would help. The problem with this particular annotation is that there is no associated import to filter on at a file level.
g

gammax

05/21/2020, 3:24 PM
You could potentially write a custom rule that tracks usages of
java.lang.SuppressWarning
. I’m not aware of nothing like that already available in Detekt
c

Charles

05/22/2020, 12:58 AM
There's definitely nothing currently available. The problem I'm having is the best approach for that particular rule. It can be applied in so many places.
b

Brais Gabin

05/22/2020, 7:41 AM
`ForbiddenAnnotation`… That could be a standard rule in detekt. Would you like to give it a try and write it?
In any case, could you open an issue to track this? if you need help writing the rule we can use the issue or this channel to help with it.
g

gammax

05/22/2020, 8:47 AM
Agree on the
ForbiddenAnnotation
. Just a note that this has a lot of overlapping with
ForbiddenImports
(that generally suffice to prevent usage of unwanted annotations). The presented case is more like an edge case as
@SuppressWarnings
has no imports.
c

Charles

05/22/2020, 3:01 PM
Yeah, I wonder how often the import rule would otherwise suffice? (But, I'll track it and see if I can come up with a concise
Rule
g

gammax

05/22/2020, 3:24 PM
The reality is that annotations can be applied on top of a lot of different identifiers (method, fields, classes, etc.). So you probably need a pretty complex rule that tracks all of them. Having using the
ForbiddenImports
will allow you to catch it regardless of the usage site of the annotation.
I'll take some time to work on this now
👏 1
g

gammax

05/22/2020, 3:47 PM
Awesome 👍
2 Views