From 57f5e45faccfac307349d8a3963bc0a1e3db8f44 Mon Sep 17 00:00:00 2001 From: Artur Bosch Date: Tue, 28 Jun 2022 13:48:06 +0200 Subject: [PATCH] Show finding at declaration name instead of the whole declaration --- .../detekt/rules/naming/MatchingDeclarationName.kt | 2 +- .../rules/naming/MatchingDeclarationNameSpec.kt | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MatchingDeclarationName.kt b/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MatchingDeclarationName.kt index cd2f1cc8c94..6acc1a419fb 100644 --- a/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MatchingDeclarationName.kt +++ b/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MatchingDeclarationName.kt @@ -79,7 +79,7 @@ class MatchingDeclarationName(config: Config = Config.empty) : Rule(config) { val declarationName = declaration.name val filename = file.fileNameWithoutSuffix() if (declarationName != filename && hasNoMatchingTypeAlias(filename)) { - val entity = Entity.from(declaration).copy(ktElement = file) + val entity = Entity.atName(declaration).copy(ktElement = file) report( CodeSmell( issue, diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MatchingDeclarationNameSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MatchingDeclarationNameSpec.kt index 123b56d201c..b5c29655987 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MatchingDeclarationNameSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/MatchingDeclarationNameSpec.kt @@ -132,7 +132,7 @@ class MatchingDeclarationNameSpec { fun `should not pass for object declaration`() { val ktFile = compileContentForTest("object O", filename = "Objects.kt") val findings = MatchingDeclarationName().lint(ktFile) - assertThat(findings).hasSourceLocation(1, 1) + assertThat(findings).hasSourceLocation(1, 8) } @Test @@ -142,14 +142,14 @@ class MatchingDeclarationNameSpec { filename = "Objects.kt" ) val findings = MatchingDeclarationName().lint(ktFile) - assertThat(findings).hasSourceLocation(1, 1) + assertThat(findings).hasSourceLocation(1, 45) } @Test fun `should not pass for class declaration`() { val ktFile = compileContentForTest("class C", filename = "Classes.kt") val findings = MatchingDeclarationName().lint(ktFile) - assertThat(findings).hasSourceLocation(1, 1) + assertThat(findings).hasSourceLocation(1, 7) } @Test @@ -163,14 +163,14 @@ class MatchingDeclarationNameSpec { filename = "ClassUtils.kt" ) val findings = MatchingDeclarationName().lint(ktFile) - assertThat(findings).hasSourceLocation(1, 1) + assertThat(findings).hasSourceLocation(1, 7) } @Test fun `should not pass for interface declaration`() { val ktFile = compileContentForTest("interface I", filename = "Not_I.kt") val findings = MatchingDeclarationName().lint(ktFile) - assertThat(findings).hasSourceLocation(1, 1) + assertThat(findings).hasSourceLocation(1, 11) } @Test @@ -184,7 +184,7 @@ class MatchingDeclarationNameSpec { filename = "E.kt" ) val findings = MatchingDeclarationName().lint(ktFile) - assertThat(findings).hasSourceLocation(1, 1) + assertThat(findings).hasSourceLocation(1, 12) } @Test @@ -211,7 +211,7 @@ class MatchingDeclarationNameSpec { val findings = MatchingDeclarationName( TestConfig("mustBeFirst" to "false") ).lint(ktFile) - assertThat(findings).hasSourceLocation(3, 1) + assertThat(findings).hasSourceLocation(3, 7) } } }