Skip to content

Commit

Permalink
Removed UnnecessaryAbstractClass if it inherits from an interface or …
Browse files Browse the repository at this point in the history
…abstract class that has concrete members

Removed deprecated annotations

Revert "Removed deprecated annotations"

This reverts commit 326a356

fix return count rule violation
  • Loading branch information
gouri-panda committed Jul 3, 2022
1 parent 2f75d2c commit 5736f42
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Expand Up @@ -21,6 +21,8 @@ import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.isAbstract
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyClassMemberScope
import org.jetbrains.kotlin.types.typeUtil.isInterface

/**
* This rule inspects `abstract` classes. In case an `abstract class` does not have any concrete members it should be
Expand Down Expand Up @@ -79,7 +81,7 @@ class UnnecessaryAbstractClass(config: Config = Config.empty) : Rule(config) {
super.visitClass(klass)
}

@Suppress("ComplexMethod")
@Suppress("ComplexMethod", "ReturnCount")
private fun KtClass.check() {
val nameIdentifier = this.nameIdentifier ?: return
if (annotationExcluder.shouldExclude(annotationEntries) || isInterface() || !isAbstract()) return
Expand All @@ -98,6 +100,8 @@ class UnnecessaryAbstractClass(config: Config = Config.empty) : Rule(config) {
report(CodeSmell(issue, Entity.from(nameIdentifier), noConcreteMember))
}
}

hasInheritedMember(true) && !isParentInterface() -> return
!hasConstructorParameter() ->
report(CodeSmell(issue, Entity.from(nameIdentifier), noConcreteMember))
else ->
Expand All @@ -122,4 +126,9 @@ class UnnecessaryAbstractClass(config: Config = Config.empty) : Rule(config) {
}
}
}
private fun KtClass.isParentInterface() =
(bindingContext[BindingContext.CLASS, this]?.unsubstitutedMemberScope as? LazyClassMemberScope)
?.supertypes
?.firstOrNull()
?.isInterface() == true
}
Expand Up @@ -86,8 +86,7 @@ class UnnecessaryAbstractClassSpec(val env: KotlinCoreEnvironment) {
}
abstract class B : A()
"""
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}

Expand Down

0 comments on commit 5736f42

Please sign in to comment.