/
UnsafeCallOnNullableType.kt
62 lines (59 loc) · 2.31 KB
/
UnsafeCallOnNullableType.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
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 org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtPostfixExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getType
import org.jetbrains.kotlin.types.typeUtil.TypeNullability
import org.jetbrains.kotlin.types.typeUtil.nullability
/**
* Reports unsafe calls on nullable types. These calls will throw a NullPointerException in case
* the nullable value is null. Kotlin provides many ways to work with nullable types to increase
* null safety. Guard the code appropriately to prevent NullPointerExceptions.
*
* <noncompliant>
* fun foo(str: String?) {
* println(str!!.length)
* }
* </noncompliant>
*
* <compliant>
* fun foo(str: String?) {
* println(str?.length)
* }
* </compliant>
*/
@RequiresTypeResolution
@ActiveByDefault(since = "1.2.0")
class UnsafeCallOnNullableType(config: Config = Config.empty) : Rule(config) {
override val issue: Issue = Issue(
"UnsafeCallOnNullableType",
Severity.Defect,
"Unsafe calls on nullable types detected. These calls will throw a NullPointerException in case " +
"the nullable value is null.",
Debt.TWENTY_MINS
)
override fun visitPostfixExpression(expression: KtPostfixExpression) {
super.visitPostfixExpression(expression)
if (expression.operationToken == KtTokens.EXCLEXCL &&
expression.baseExpression?.getType(bindingContext)?.nullability() == TypeNullability.NULLABLE
) {
report(
CodeSmell(
issue,
Entity.from(expression),
"Calling !! on a nullable type will throw a " +
"NullPointerException at runtime in case the value is null. It should be avoided."
)
)
}
}
}