Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only report UnsafeCallOnNullableType on actual nullable types (#1886)
* Only check !! used on nullable types * Use more appropriate method to get a type
- Loading branch information
Showing
4 changed files
with
44 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 31 additions & 13 deletions
44
...es/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/UnsafeCallOnNullableTypeSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,55 @@ | ||
package io.gitlab.arturbosch.detekt.rules.bugs | ||
|
||
import io.gitlab.arturbosch.detekt.test.compileAndLint | ||
import io.gitlab.arturbosch.detekt.test.KtTestCompiler | ||
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.specification.describe | ||
|
||
class UnsafeCallOnNullableTypeSpec : Spek({ | ||
val subject by memoized { UnsafeCallOnNullableType() } | ||
|
||
val wrapper by memoized( | ||
factory = { KtTestCompiler.createEnvironment() }, | ||
destructor = { it.dispose() } | ||
) | ||
|
||
describe("check all variants of safe/unsafe calls on nullable types") { | ||
|
||
it("reports unsafe call on nullable type") { | ||
val code = """ | ||
fun test(str: String?) { | ||
println(str!!.length) | ||
}""" | ||
assertThat(subject.compileAndLint(code)).hasSize(1) | ||
fun test(str: String?) { | ||
println(str!!.length) | ||
} | ||
""" | ||
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1) | ||
} | ||
|
||
it("does not report unsafe call on platform type") { | ||
val code = """ | ||
import java.util.UUID | ||
val version = UUID.randomUUID()!! | ||
""" | ||
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty() | ||
} | ||
|
||
it("does not report safe call on nullable type") { | ||
val code = """ | ||
fun test(str: String?) { | ||
println(str?.length) | ||
}""" | ||
assertThat(subject.compileAndLint(code)).hasSize(0) | ||
fun test(str: String?) { | ||
println(str?.length) | ||
} | ||
""" | ||
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty() | ||
} | ||
|
||
it("does not report safe call in combination with the elvis operator") { | ||
val code = """ | ||
fun test(str: String?) { | ||
println(str?.length ?: 0) | ||
}""" | ||
assertThat(subject.compileAndLint(code)).hasSize(0) | ||
fun test(str: String?) { | ||
println(str?.length ?: 0) | ||
} | ||
""" | ||
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty() | ||
} | ||
} | ||
}) |