Skip to content

Commit

Permalink
UnusedPrivateMember: fix false negative with named arguments (#5374)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kameyama committed Oct 5, 2022
1 parent b324516 commit b4710a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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()
}
}
}

0 comments on commit b4710a2

Please sign in to comment.