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

UnusedPrivateMember: fix false negative with named arguments #5374

Merged
merged 1 commit into from Oct 5, 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 @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyDelegate
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.psi.KtSecondaryConstructor
import org.jetbrains.kotlin.psi.KtValueArgumentName
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isPrivate
Expand Down Expand Up @@ -258,7 +259,9 @@ private class UnusedParameterVisitor(allowedNames: Regex) : UnusedMemberVisitor(
}

override fun visitReferenceExpression(expression: KtReferenceExpression) {
parameters.remove(expression.text.removeSurrounding("`"))
if (expression.parent !is KtValueArgumentName) {
parameters.remove(expression.text.removeSurrounding("`"))
}
super.visitReferenceExpression(expression)
}
})
Expand Down
Expand Up @@ -1648,4 +1648,35 @@ class UnusedPrivateMemberSpec(val env: KotlinCoreEnvironment) {
assertThat(subject.lint(code)).hasSize(1).hasStartSourceLocation(6, 9)
}
}

@Nested
inner class `parameter with the same name as a named argument #5373` {
@Test
fun `unused parameter`() {
val code = """
fun foo(modifier: Int) {
bar(modifier = 1)
}

fun bar(modifier: Int) {
println(modifier)
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1).hasStartSourceLocation(1, 9)
}

@Test
fun `used parameter`() {
val code = """
fun foo(modifier: Int) {
bar(modifier = modifier)
}

fun bar(modifier: Int) {
println(modifier)
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
}