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

NamedArguments: don't count trailing lambda argument #5002

Merged
merged 1 commit into from Jun 28, 2022
Merged
Show file tree
Hide file tree
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 @@ -42,15 +42,15 @@ class NamedArguments(config: Config = Config.empty) : Rule(config) {
Debt.FIVE_MINS
)

@Configuration("number of parameters that triggers this inspection")
@Configuration("number of arguments that triggers this inspection")
private val threshold: Int by config(defaultValue = 3)

@Configuration("ignores when argument values are the same as the parameter names")
private val ignoreArgumentsMatchingNames: Boolean by config(defaultValue = false)

override fun visitCallExpression(expression: KtCallExpression) {
if (bindingContext == BindingContext.EMPTY) return
val valueArguments = expression.valueArguments
val valueArguments = expression.valueArguments.filterNot { it is KtLambdaArgument }
if (valueArguments.size > threshold && expression.canNameArguments()) {
val message = "This function call has ${valueArguments.size} arguments. To call a function with more " +
"than $threshold arguments you should set the name of each argument."
Expand Down
Expand Up @@ -196,6 +196,18 @@ class NamedArgumentsSpec(val env: KotlinCoreEnvironment) {
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}

@Test
fun `does not count lambda argument`() {
val code = """
fun test(n: Int) {
require(n == 2) { "N is not 2" }
}
"""
val subject = NamedArguments(TestConfig(mapOf("threshold" to 1)))
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(0)
}
}

@Nested
Expand Down