From 95ea613f2fc611ede135f74635aea8b02e9571f9 Mon Sep 17 00:00:00 2001 From: Thomas Keller Date: Mon, 20 Jul 2020 15:26:28 +0200 Subject: [PATCH] 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 --- .../arturbosch/detekt/rules/KtValueArgument.kt | 2 +- .../detekt/rules/style/UseCheckOrErrorSpec.kt | 12 ++++++++++++ .../arturbosch/detekt/rules/style/UseRequireSpec.kt | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/detekt-psi-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KtValueArgument.kt b/detekt-psi-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KtValueArgument.kt index d8361be1a..f350162ab 100644 --- a/detekt-psi-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KtValueArgument.kt +++ b/detekt-psi-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KtValueArgument.kt @@ -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 } diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckOrErrorSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckOrErrorSpec.kt index e562d1982..210c26eb4 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckOrErrorSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckOrErrorSpec.kt @@ -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) + } } } }) diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireSpec.kt index 9b6ae5c75..446cb32a5 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireSpec.kt @@ -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") {