Skip to content

Commit

Permalink
Show deprecation message (detekt#6614)
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin authored and mgroth0 committed Feb 11, 2024
1 parent f2f1540 commit 6b89f56
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ class Deprecation(config: Config) : Rule(config) {
override val defaultRuleIdAliases = setOf("DEPRECATION")

override fun visitElement(element: PsiElement) {
if (hasDeprecationCompilerWarnings(element)) {
val diagnostic = hasDeprecationCompilerWarnings(element)
if (diagnostic != null) {
val entity = if (element is KtNamedDeclaration) Entity.atName(element) else Entity.from(element)
report(CodeSmell(issue, entity, "${element.text} is deprecated."))
report(CodeSmell(issue, entity, """${element.text} is deprecated with message "${diagnostic.b}""""))
}
super.visitElement(element)
}

private fun hasDeprecationCompilerWarnings(element: PsiElement) =
bindingContext.diagnostics
.forElement(element)
.any { it.factory == Errors.DEPRECATION }
.firstOrNull { it.factory == Errors.DEPRECATION }
?.let { Errors.DEPRECATION.cast(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DeprecationSpec(private val env: KotlinCoreEnvironment) {
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings.first().message).isEqualTo("Foo is deprecated.")
assertThat(findings.first().message).isEqualTo("""Foo is deprecated with message "deprecation message"""")
}

@Test
Expand Down

0 comments on commit 6b89f56

Please sign in to comment.