Can I use detekt to prevent the use of a field in ...
# detekt
c
Can I use detekt to prevent the use of a field in one of my classes? Typically I would make the field private, but in this case I can't. so i just want to warn my team.
b
The first idea is deprecation
We had a forbidden method call but I think that it doesn't work for fields or properties
I think that we have an issue requesting that
c
hm. that might work.
l
My first idea is using
_
to designate fields that one shouldn't touch.
val _aDelegate = abc()
val a by aDelegate
c
hm. apparently you can forbid properties? https://github.com/detekt/detekt/pull/5078/files
gotta look into it though.
b
Nice! I didn't remember that pr. Then you have lots of options :)
c
Am I going crazy, or are the docs for setting up forbiddenMethodCall wrong? https://detekt.dev/docs/rules/style/#forbiddenmethodcall How the heck am I suppose to mark a field as forbidden?
b
Looking at the PR you linked you can not forbid a "field" you can forbid the
get
and the
set
of a property. If there are issues with the documentation any PR is more than welcome. And, if you don't know how to do something we will need more information than that to try to help you.
c
yeah. apparently setting methods as an array doesn't work. but you can just set a -method -method -method so I tried going that route, but still no luck anyway because like you said. a field apparently can't be prevented. going to to to write a lint rule instead. thanks
👍 1