Is it possible to have a rule that disallows all u...
# ktlint
h
Is it possible to have a rule that disallows all use of
!!
?
r
Yes, but it has nothing to do with code formatting, which is the primary goal of ktlint, so has to be a custom rule
g
We do have a rule for that on #detekt:
UnsafeCallOnNullableType
👍 1
h
@gammax Cool! We use kotlinter/ktlint for now, but I have been considering swapping it out. In the mean time I'll look at creating that custom rule (or finding someone else who've done the same). @romtsn Any ideas how to match
!!
in a
Rule
?
r
swapping out should be fairly easy, as detekt is wrapping ktlint for formatting, so you are getting same but more
☝️ 1
👍 1
except that you're losing on code formatting
g
☝️ Actually you can also do code formatting (via KtLint) through Detekt.
h
ah, cool. because the formatter in kotlinter is both fast and nice
r
@gammax didn't know it's possible, mind sharing a link on how to do that?
except using IDE integration
@henrik anyway if you're gonna go for you custom rule, I'd say the closest one to that is https://github.com/pinterest/ktlint/blob/master/ktlint-ruleset-template/src/main/kotlin/yourpkgname/NoVarRule.kt except that you need to use `ElementType.EXCLEXCL`instead
g
@romtsn You can do it via the
autoCorrect
property: https://detekt.github.io/detekt/extensions.html#autocorrect-property All the
detekt-formatting
rules implement that property and delegate to KtLint for the reformatting of the files.
h
@romtsn Thanks! Very helpful 🙂
r
This, however, will just report any use of
!!
, it will be not easy to have something similar to detekt, cause they leverage kotlin compiler analysis and we don;t have mechanisms for that yet
h
@romtsn So
val str = "Foo!!"
would match?
r
yeah but that you can avoid by checking if it's a string or a comment and so on
@gammax thanks, wasn't expecting it in this section tbh 🙂 but rather under rulesets/formatting or so
h
Ah, that helps.
g
but rather under rulesets/formatting or so
Yeah you’re right 😞 I believe we should impove a bit on our docs for the formatting module
r
I think we need to do overhaul for ktlint, having a staticdocs site would be awesome, similar to detekt 😄 for now readme is a black hole in ktlint
👍 1