Brian Hartvigsen
05/10/2023, 10:38 PM@RequiresTypeResolution
class PreferLetOverIfNotNullElseNull(
config: Config = Config.empty
) : Rule(config) {
override val issue = Issue(
"PreferLetOverIfNotNullElseNull",
Severity.Style,
"Using `if (X == null) { } else null` is just a longer form of `X?.let { }`",
Debt.FIVE_MINS
)
override val active: Boolean
get() = true
override fun visitIfExpression(expression: KtIfExpression) {
super.visitIfExpression(expression)
val elseBranch = expression.`else` ?: return
val condition = expression.condition as? KtBinaryExpression
if (condition?.isNonNullCheck() == true && isNullConstant(elseBranch)) {
report(CodeSmell(issue, Entity.from(expression), issue.description))
}
}
}
Brian Hartvigsen
05/10/2023, 10:38 PMvisitIfExpression
function and it doesn't appear to be getting called at allBrian Hartvigsen
05/10/2023, 10:39 PMoverride val active
was just my latest attempt, I don't know how to tell if my plugin is even getting called)Brian Hartvigsen
05/10/2023, 10:40 PMBrian Hartvigsen
05/10/2023, 10:56 PMSeverity.Style
and didn't have style checks enabled... 🤦♂️Brian Hartvigsen
05/10/2023, 11:10 PMBrian Hartvigsen
05/10/2023, 11:15 PMNoIfNotNullElseNull
works, but if I change the name to PreferLetOverIfNotNullElseNull
the rule fails to processBrais Gabin
05/11/2023, 9:36 AMIs this the best place to ask about a plugin I’ve developed?I’m not sure if it is the best, but it is a good one for sure 😉 This seems as an error on the configuration. when you change the name of the rule are you editting your configuration to ensure the rule and the rule set are enabled? And, for testing, I would recommend you to set a unconditional report somewhere so you know that every-single-file should be reported so the only thing that could be not working is the configuration. Once the configuration is working you can return to the real behaviour.
Brais Gabin
05/11/2023, 9:38 AMAtul Gupta
05/11/2023, 11:01 AMBrais Gabin
05/11/2023, 12:16 PMAtul Gupta
05/11/2023, 12:50 PMif (X == null) { } else null is just a longer form of X?.let { }
This rule prevent thisBrais Gabin
05/11/2023, 1:37 PMlet
because people usually overuse it and a rule that enforce you to use it… I’m not 100% sure if is good. But for sure I would always use the let
form on this case. We could open an issue and look for what other people thinkBrian Hartvigsen
05/11/2023, 9:11 PMCouldBeLet
)Brian Hartvigsen
05/11/2023, 9:11 PMBrian Hartvigsen
05/11/2023, 9:58 PMBrian Hartvigsen
05/11/2023, 9:58 PM