New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule for method nesting #4780
Comments
Extract from: https://kotlinlang.org/docs/scope-functions.html#function-selection
I agree that we should have a rule that flags if any of the scoped functions are scoped. |
I really like this idea for a new rule. Maybe, the rule should be configurable with a threshold. But, I wouldn't see this as strictly mandatory for a first inclusion in detekt. @WildOrangutan may I ask you to take this issue and submit a first PR for this new rule? |
I will give it a go, but won't promise anything :) I'm thinking of a rule like below. Let me know if that sounds ok.
|
Sounds great. I would vote to add let to the list too. |
How is that different than // ...
override fun visitCallExpression(expression: KtCallExpression) {
val lambdaArguments = expression.lambdaArguments
if (expression.isUsedForNesting()) {
insideLambdaDo(lambdaArguments) { inc() }
super.visitCallExpression(expression)
insideLambdaDo(lambdaArguments) { dec() }
}
}
fun KtCallExpression.isUsedForNesting(): Boolean = when (getCallNameExpression()?.text) {
"run", "let", "apply", "with", "use", "forEach" -> true
else -> false
} |
The problem of that rule is that if you set the threshold to 2 and you nes two ifs it will yield. And that's not the point of this rule. I don't care about nesting ifs or fors. I care about messing scoped functions. But we could find a way to change the behavior. |
Hey. I'm wondering if it would be possible to add a rule, to avoid nesting certain methods/functions.
For example:
This kind of practice is often confusing, since you don't know where
foo()
belongs.I've considered using
ComplexMethod
, but it seems like you need to pass certain threshold, in order for detekt to fail. I would like to fail instantly in my example.The text was updated successfully, but these errors were encountered: