Skip to content
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

Refactor MultilineLambdaItParameter rule #4428

Merged
merged 1 commit into from Dec 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -76,38 +76,34 @@ class MultilineLambdaItParameter(val config: Config) : Rule(config) {
override fun visitLambdaExpression(lambdaExpression: KtLambdaExpression) {
super.visitLambdaExpression(lambdaExpression)
if (bindingContext == BindingContext.EMPTY) return
// If the lambda expression has <= 1 statements, skip check.
if (lambdaExpression.bodyExpression?.statements?.size ?: 0 <= 1) return
val size = lambdaExpression.bodyExpression?.statements?.size
if (size == null || size <= 1) return

val parameterNames = lambdaExpression.valueParameters.map { it.name }
when {
// Explicit `it`
IT_LITERAL in parameterNames ->
// Explicit `it`
if (IT_LITERAL in parameterNames) {
report(
CodeSmell(
issue,
Entity.from(lambdaExpression),
"The parameter name in a multiline lambda should not be an explicit `it`. " +
"Consider giving your parameter a readable and descriptive name."
)
)
} else if (parameterNames.isEmpty()) { // Implicit `it`
val implicitParameter = lambdaExpression.implicitParameter(bindingContext)
if (implicitParameter != null &&
lambdaExpression.hasImplicitParameterReference(implicitParameter, bindingContext)
) {
report(
CodeSmell(
issue,
Entity.from(lambdaExpression),
"The parameter name in a multiline lambda should not be an explicit `it`. " +
"The implicit `it` should not be used in a multiline lambda. " +
"Consider giving your parameter a readable and descriptive name."
)
)
// Implicit `it`
parameterNames.isEmpty() -> {
val implicitParameter = lambdaExpression.implicitParameter(bindingContext)
if (implicitParameter != null &&
lambdaExpression.hasImplicitParameterReference(implicitParameter, bindingContext)
) {
report(
CodeSmell(
issue,
Entity.from(lambdaExpression),
"The implicit `it` should not be used in a multiline lambda. " +
"Consider giving your parameter a readable and descriptive name."
)
)
}
}
else -> { }
}
}
}