Skip to content

Commit

Permalink
UnusedPrivateMember: highlight declaration name (#4928)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kameyama committed Jun 7, 2022
1 parent 9146e4d commit 0ecebd4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Expand Up @@ -153,7 +153,7 @@ private class UnusedFunctionVisitor(
else -> emptyList()
}
unusedFunctions.map {
CodeSmell(issue, Entity.from(it), "Private function `$functionName` is unused.")
CodeSmell(issue, Entity.atName(it), "Private function `$functionName` is unused.")
}
}
}
Expand Down Expand Up @@ -212,7 +212,7 @@ private class UnusedParameterVisitor(allowedNames: Regex) : UnusedMemberVisitor(

override fun getUnusedReports(issue: Issue): List<CodeSmell> {
return unusedParameters.map {
CodeSmell(issue, Entity.from(it), "Function parameter `${it.nameAsSafeName.identifier}` is unused.")
CodeSmell(issue, Entity.atName(it), "Function parameter `${it.nameAsSafeName.identifier}` is unused.")
}
}

Expand Down Expand Up @@ -284,7 +284,7 @@ private class UnusedPropertyVisitor(allowedNames: Regex) : UnusedMemberVisitor(a
.map {
CodeSmell(
issue,
Entity.from(it),
Entity.atName(it),
"Private property `${it.nameAsSafeName.identifier}` is unused."
)
}
Expand Down
Expand Up @@ -1321,7 +1321,7 @@ class UnusedPrivateMemberSpec(val env: KotlinCoreEnvironment) {
"""
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1).hasSourceLocations(
SourceLocation(3, 5)
SourceLocation(3, 30)
)
}
}
Expand Down Expand Up @@ -1593,4 +1593,48 @@ class UnusedPrivateMemberSpec(val env: KotlinCoreEnvironment) {
assertThat(subject.lintWithContext(env, code)).hasSize(0)
}
}

@Nested
inner class `highlights declaration name` {
@Test
fun function() {
val code = """
class Test {
/**
* kdoc
*/
private fun foo() = 1
}
"""
assertThat(subject.lint(code)).hasSize(1).hasSourceLocation(5, 17)
}

@Test
fun property() {
val code = """
class Test {
/**
* kdoc
*/
private val foo = 1
}
"""
assertThat(subject.lint(code)).hasSize(1).hasSourceLocation(5, 17)
}

@Test
fun parameter() {
val code = """
class Test {
fun test(
/**
* kdoc
*/
x: Int
) = 1
}
"""
assertThat(subject.lint(code)).hasSize(1).hasSourceLocation(6, 9)
}
}
}

0 comments on commit 0ecebd4

Please sign in to comment.