/
UnreachableCatchBlock.kt
82 lines (76 loc) · 2.81 KB
/
UnreachableCatchBlock.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import io.gitlab.arturbosch.detekt.rules.safeAs
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.psi.KtCatchClause
import org.jetbrains.kotlin.psi.KtTryExpression
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.isSubclassOf
/**
* Reports unreachable catch blocks.
* Catch blocks can be unreachable if the exception has already been caught in the block above.
*
* <noncompliant>
* fun test() {
* try {
* foo()
* } catch (t: Throwable) {
* bar()
* } catch (e: Exception) {
* // Unreachable
* baz()
* }
* }
* </noncompliant>
*
* <compliant>
* fun test() {
* try {
* foo()
* } catch (e: Exception) {
* baz()
* } catch (t: Throwable) {
* bar()
* }
* }
* </compliant>
*
*/
@RequiresTypeResolution
@ActiveByDefault(since = "1.21.0")
class UnreachableCatchBlock(config: Config = Config.empty) : Rule(config) {
override val issue = Issue(
javaClass.simpleName,
Severity.Warning,
"Unreachable catch block detected.",
Debt.FIVE_MINS
)
@Suppress("ReturnCount")
override fun visitCatchSection(catchClause: KtCatchClause) {
super.visitCatchSection(catchClause)
val tryExpression = catchClause.getStrictParentOfType<KtTryExpression>() ?: return
val prevCatchClauses = tryExpression.catchClauses.takeWhile { it != catchClause }
if (prevCatchClauses.isEmpty()) return
val catchClassDescriptor = catchClause.catchClassDescriptor() ?: return
if (prevCatchClauses.any { catchClassDescriptor.isSubclassOf(it) }) {
report(CodeSmell(issue, Entity.from(catchClause), "This catch block is unreachable."))
}
}
private fun KtCatchClause.catchClassDescriptor(): ClassDescriptor? {
val typeReference = catchParameter?.typeReference ?: return null
return bindingContext[BindingContext.TYPE, typeReference]?.constructor?.declarationDescriptor?.safeAs()
}
private fun ClassDescriptor.isSubclassOf(catchClause: KtCatchClause): Boolean {
val catchClassDescriptor = catchClause.catchClassDescriptor() ?: return false
return isSubclassOf(catchClassDescriptor)
}
}