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

Improve UnnamedParameterUse message (change CALL_EXPRESSION to actual function name) #6681

Merged
merged 4 commits into from Dec 5, 2023
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 @@ -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