Skip to content

Commit

Permalink
Improve UnnamedParameterUse message (change CALL_EXPRESSION to actual…
Browse files Browse the repository at this point in the history
… function name) (detekt#6681)

* Add test for current behavior and shorten message

* Focus on the function call for reporting

* Report the function name in the message rather than CALL_EXPRESSION + grammar

* Always report something
  • Loading branch information
TWiStErRob authored and mgroth0 committed Feb 11, 2024
1 parent c28041b commit 427fa44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Expand Up @@ -75,6 +75,7 @@ import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
*/
@RequiresTypeResolution
class UnnamedParameterUse(config: Config = Config.empty) : Rule(config) {

override val issue = Issue(
javaClass.simpleName,
"Passing no named parameters can cause issue when parameters order change",
Expand Down Expand Up @@ -121,12 +122,12 @@ class UnnamedParameterUse(config: Config = Config.empty) : Rule(config) {
}

if (namedArgumentList.any { it.first.not() }) {
val target = expression.calleeExpression ?: expression
report(
CodeSmell(
issue,
Entity.from(expression),
"$expression uses unnamed parameters. Consider using named parameters as they make usage " +
"of function more safe"
Entity.from(target),
"Consider using named parameters in ${target.text} as they make usage of the function more safe."
)
)
}
Expand Down
Expand Up @@ -89,6 +89,23 @@ class UnnamedParameterUseSpec(private val env: KotlinCoreEnvironment) {
).hasSize(1)
}

@Test
fun `report uses the function name as location and message`() {
val code = """
fun f(enabled: Boolean, shouldLog: Boolean) {
if (shouldLog) println(enabled)
}
fun test() {
f(false, true)
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code))
.hasTextLocations(102 to 103)
.singleElement()
.hasMessage("Consider using named parameters in f as they make usage of the function more safe.")
}

@Test
fun `does not report two unnamed param when both are same`() {
val code = """
Expand Down

0 comments on commit 427fa44

Please sign in to comment.