Skip to content

Commit

Permalink
Handle corner case of multiple inheritance in UnnecessaryAbstractClas…
Browse files Browse the repository at this point in the history
…s rule
  • Loading branch information
amitdash291 committed Jul 9, 2022
1 parent 675c48c commit 69b3fd3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class UnnecessaryAbstractClass(config: Config = Config.empty) : Rule(config) {
val members = members()
when {
members.isNotEmpty() -> checkMembers(members, nameIdentifier)
hasInheritedMember(true) && !isParentInterface() -> return
hasInheritedMember(true) && isAnyParentAbstract() -> return
!hasConstructorParameter() ->
report(CodeSmell(issue, Entity.from(nameIdentifier), noConcreteMember))
else ->
Expand Down Expand Up @@ -129,9 +129,8 @@ class UnnecessaryAbstractClass(config: Config = Config.empty) : Rule(config) {
}
}

private fun KtClass.isParentInterface() =
private fun KtClass.isAnyParentAbstract() =
(bindingContext[BindingContext.CLASS, this]?.unsubstitutedMemberScope as? LazyClassMemberScope)
?.supertypes
?.firstOrNull()
?.isInterface() == true
?.all { it.isInterface() } == false
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class UnnecessaryAbstractClassSpec(val env: KotlinCoreEnvironment) {
}

@Test
fun `does not report abstract class that inherits from an interface and an abstract class in that order`() {
fun `does not report abstract class that inherits from an abstract class and an interface in that order`() {
val code = """
interface I
Expand All @@ -103,6 +103,21 @@ class UnnecessaryAbstractClassSpec(val env: KotlinCoreEnvironment) {
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}

@Test
fun `does not report abstract class that inherits from an interface and an abstract class in that order`() {
val code = """
interface I
@Deprecated("We don't care about this first class")
abstract class A {
abstract val i: Int
}
abstract class B: I, A()
"""
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
}

@Test
Expand Down

0 comments on commit 69b3fd3

Please sign in to comment.