Check string arguments with TR enabled (#2879)

* Check string arguments with TR enabled

Check simple string arguments even when type resolution is enabled.
This prevented the `UseRequire` and `UseCheckOrError` checks find issues with TR enabled.

* Add tests

Co-authored-by: Thomas Keller <thomas.keller-extern@deutschebahn.com>
This commit is contained in:
Thomas Keller
2020-07-20 15:26:28 +02:00
committed by GitHub
parent 2de31f1b7e
commit 95ea613f2f
3 changed files with 21 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ fun KtValueArgument.isString(bindingContext: BindingContext): Boolean {
val argumentExpression = getArgumentExpression()
return if (bindingContext != BindingContext.EMPTY) {
val type = argumentExpression?.getResolvedCall(bindingContext)?.resultingDescriptor?.returnType
type != null && KotlinBuiltIns.isString(type)
argumentExpression is KtStringTemplateExpression || type != null && KotlinBuiltIns.isString(type)
} else {
argumentExpression is KtStringTemplateExpression
}

View File

@@ -174,6 +174,18 @@ class UseCheckOrErrorSpec : Spek({
"""
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports if the exception thrown has a string literal argument") {
val code = """
fun test(throwable: Throwable) {
when(throwable) {
is NumberFormatException -> println("a")
else -> throw IllegalStateException("b")
}
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
}
})

View File

@@ -146,6 +146,14 @@ class UseRequireSpec : Spek({
"""
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports if the exception thrown has a String literal argument") {
val code = """
fun test(throwable: Throwable) {
if (throwable !is NumberFormatException) throw IllegalArgumentException("a")
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
context("throw is not after a precondition") {