forked from detekt/detekt
/
UnnecessaryNotNullCheckSpec.kt
158 lines (143 loc) · 4.97 KB
/
UnnecessaryNotNullCheckSpec.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.lintWithContext
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
@KotlinCoreEnvironmentTest
class UnnecessaryNotNullCheckSpec(private val env: KotlinCoreEnvironment) {
private val subject = UnnecessaryNotNullCheck()
@Nested
inner class `check unnecessary not null checks` {
@Test
fun shouldDetectNotNullCallOnVariable() {
val code = """
val x = 5
val y = requireNotNull(x)
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(18 to 35)
}
@Test
fun shouldDetectNotNullCallOnVariableUsingCheckNotNull() {
val code = """
val x = 5
val y = checkNotNull(x)
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(18 to 33)
}
@Test
fun shouldDetectNotNullCallOnFunctionReturn() {
val code = """
fun foo(): Int {
return 5
}
fun bar() {
requireNotNull(foo())
}
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(48 to 69)
}
@Test
fun shouldDetectWhenCallingDefinitelyNonNullableGenericFunction() {
val code = """
fun <T> foo(x: T & Any): T & Any {
return x
}
fun bar() {
requireNotNull(foo(5))
}
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(66 to 88)
}
@Test
fun shouldDetectWhenCallingPrimitiveJavaMethod() {
val code = """
fun foo() {
requireNotNull(System.currentTimeMillis())
}
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(16 to 58)
}
}
@Nested
inner class `check valid not null check usage` {
@Test
fun shouldIgnoreNotNullCallOnNullableVariableWithValue() {
val code = """
val x: Int? = 5
val y = requireNotNull(x)
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun shouldIgnoreNotNullCallOnNullableVariableWithNull() {
val code = """
val x: Int? = null
val y = requireNotNull(x)
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun shouldIgnoreNotNullCallOnNullableFunctionReturnWithValue() {
val code = """
fun foo(): Int? {
return 5
}
fun bar() {
requireNotNull(foo())
}
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun shouldIgnoreNotNullCallOnNullableFunctionReturnWithNull() {
val code = """
fun foo(): Int? {
return null
}
fun bar() {
requireNotNull(foo())
}
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun shouldIgnoreWhenCallingNullableGenericFunction() {
val code = """
fun <T> foo(x: T): T {
return x
}
fun bar() {
requireNotNull(foo<Int?>(5))
}
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun shouldIgnoreWhenCallingObjectJavaMethod() {
val code = """
fun foo() {
requireNotNull(System.getLogger())
}
""".trimIndent()
val findings = subject.lintWithContext(env, code)
assertThat(findings).isEmpty()
}
}
}