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

Rajat Varyani

05/13/2020, 4:26 PM
Hi 👋 . I am trying to write to a custom rule to detect mutation in code. This involves checking for presence of Mutable collections, keywords like
var
in codebase. What is i could come up till now is
Copy code
override fun visitDeclaration(dcl: KtDeclaration) {
        super.visitDeclaration(dcl)
        if (dcl is KtProperty && dcl.isVar) {
            report(CodeSmell(issue, Entity.from(dcl), "Mutation in code"))
        }
    }
Now I am writing code to detect keywords like
Copy code
val list = mutableListOf(1, 2)
. I am not sure how to go about it?
b

Brais Gabin

05/13/2020, 4:39 PM
Detekt doesn’t have a `ForbiddenClass`… I suppose that usually all the classes are used with an
import
stament so it’s easier to forbid the import… But ForbiddenImport doesn’t help you…
Maybe you can check the code in
ExplicitCollectionElementAccessMethod
. It looks for uses of Map so probably you can take some ideas from it.
r

Rajat Varyani

05/14/2020, 5:20 AM
Thanks. Let me try that.