Skip to content

Commit

Permalink
Report LabeledExpression as the label instead of the whole expression (
Browse files Browse the repository at this point in the history
…#5351)

Closes #5316
  • Loading branch information
arturbosch committed Sep 25, 2022
1 parent 7b73606 commit 121b0d8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Expand Up @@ -76,10 +76,10 @@ class LabeledExpression(config: Config = Config.empty) : Rule(config) {
override fun visitExpressionWithLabel(expression: KtExpressionWithLabel) {
super.visitExpressionWithLabel(expression)
if (expression !is KtThisExpression || isNotReferencingOuterClass(expression)) {
expression.getLabelName()?.let { labelName ->
if (ignoredLabels.none { labelName.contains(it, ignoreCase = true) }) {
report(CodeSmell(issue, Entity.from(expression), issue.description))
}
val label = expression.getTargetLabel()
val labelName = label?.getReferencedName()
if (labelName != null && ignoredLabels.none { labelName.contains(it, ignoreCase = true) }) {
report(CodeSmell(issue, Entity.from(label), issue.description))
}
}
}
Expand Down
@@ -1,8 +1,8 @@
package io.gitlab.arturbosch.detekt.rules.complexity

import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class LabeledExpressionSpec {
Expand Down Expand Up @@ -34,7 +34,11 @@ class LabeledExpressionSpec {
}
}
""".trimIndent()
assertThat(subject.compileAndLint(code)).hasSize(1)

val findings = subject.compileAndLint(code)

assertThat(findings).hasSize(1)
assertThat(findings).hasStartSourceLocation(3, 28)
}

@Test
Expand Down

0 comments on commit 121b0d8

Please sign in to comment.