From 20eb8227471b180a35e25b3ee143d350ba4272fc Mon Sep 17 00:00:00 2001 From: Artur Bosch Date: Sun, 25 Sep 2022 10:59:43 +0200 Subject: [PATCH] Report CastToNullableType at the cast operator instead of the whole expression --- .../gitlab/arturbosch/detekt/rules/bugs/CastToNullableType.kt | 2 +- .../arturbosch/detekt/rules/bugs/CastToNullableTypeSpec.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/CastToNullableType.kt b/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/CastToNullableType.kt index 2e9c3231441..76f77fb282f 100644 --- a/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/CastToNullableType.kt +++ b/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/CastToNullableType.kt @@ -47,6 +47,6 @@ class CastToNullableType(config: Config = Config.empty) : Rule(config) { val message = "Use the safe cast ('as? ${nullableTypeElement.innerType?.text}')" + " instead of 'as ${nullableTypeElement.text}'." - report(CodeSmell(issue, Entity.from(expression), message)) + report(CodeSmell(issue, Entity.from(operationReference), message)) } } diff --git a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/CastToNullableTypeSpec.kt b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/CastToNullableTypeSpec.kt index 02e832c6b45..b112884f9e3 100644 --- a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/CastToNullableTypeSpec.kt +++ b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/CastToNullableTypeSpec.kt @@ -16,7 +16,7 @@ class CastToNullableTypeSpec { """.trimIndent() val findings = subject.compileAndLint(code) assertThat(findings).hasSize(1) - assertThat(findings).hasStartSourceLocation(2, 22) + assertThat(findings).hasStartSourceLocation(2, 24) assertThat(findings[0]).hasMessage("Use the safe cast ('as? String') instead of 'as String?'.") }