Skip to content

Commit

Permalink
UnnecessaryAbstractClass: report only the class name (#4808)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kameyama committed May 4, 2022
1 parent 30af7dc commit 61cfc24
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Expand Up @@ -79,27 +79,29 @@ class UnnecessaryAbstractClass(config: Config = Config.empty) : Rule(config) {
super.visitClass(klass)
}

@Suppress("ComplexMethod")
private fun KtClass.check() {
val nameIdentifier = this.nameIdentifier ?: return
if (annotationExcluder.shouldExclude(annotationEntries) || isInterface() || !isAbstract()) return
val members = members()
when {
members.isNotEmpty() -> {
val (abstractMembers, concreteMembers) = members.partition { it.isAbstract() }
if (abstractMembers.isEmpty() && !hasInheritedMember(true)) {
report(CodeSmell(issue, Entity.from(this), noAbstractMember))
report(CodeSmell(issue, Entity.from(nameIdentifier), noAbstractMember))
return
}
if (abstractMembers.any { it.isInternal() || it.isProtected() } || hasConstructorParameter()) {
return
}
if (concreteMembers.isEmpty() && !hasInheritedMember(false)) {
report(CodeSmell(issue, Entity.from(this), noConcreteMember))
report(CodeSmell(issue, Entity.from(nameIdentifier), noConcreteMember))
}
}
!hasConstructorParameter() ->
report(CodeSmell(issue, Entity.from(this), noConcreteMember))
report(CodeSmell(issue, Entity.from(nameIdentifier), noConcreteMember))
else ->
report(CodeSmell(issue, Entity.from(this), noAbstractMember))
report(CodeSmell(issue, Entity.from(nameIdentifier), noAbstractMember))
}
}

Expand Down
Expand Up @@ -3,8 +3,8 @@ package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
Expand All @@ -31,6 +31,7 @@ class UnnecessaryAbstractClassSpec(val env: KotlinCoreEnvironment) {
"""
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
assertThat(findings).hasSourceLocation(1, 16)
}

@Nested
Expand All @@ -40,6 +41,7 @@ class UnnecessaryAbstractClassSpec(val env: KotlinCoreEnvironment) {
val code = "abstract class A"
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
assertThat(findings).hasSourceLocation(1, 16)
}

@Test
Expand Down Expand Up @@ -173,6 +175,7 @@ class UnnecessaryAbstractClassSpec(val env: KotlinCoreEnvironment) {
val code = "abstract class A(val i: Int)"
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
assertThat(findings).hasSourceLocation(1, 16)
}

@Test
Expand All @@ -182,6 +185,14 @@ class UnnecessaryAbstractClassSpec(val env: KotlinCoreEnvironment) {
assertFindingMessage(findings, message)
}

@Test
fun `reports no abstract members in an abstract class with just a constructor parameter`() {
val code = "abstract class A(i: Int)"
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
assertThat(findings).hasSourceLocation(1, 16)
}

@Test
fun `reports an abstract class with no abstract member derived from a class with abstract members`() {
val code = """
Expand Down Expand Up @@ -282,5 +293,5 @@ class UnnecessaryAbstractClassSpec(val env: KotlinCoreEnvironment) {

private fun assertFindingMessage(findings: List<Finding>, message: String) {
assertThat(findings).hasSize(1)
assertThat(findings.first().message).isEqualTo(message)
assertThat(findings.first()).hasMessage(message)
}

0 comments on commit 61cfc24

Please sign in to comment.