From 23fd0cfe5cc989be81a7bc647b57fd03c522e94a Mon Sep 17 00:00:00 2001 From: Pleshkova Daria Date: Thu, 14 Dec 2023 16:30:58 +0300 Subject: [PATCH] ### What's done: - Fixed `KDOC_WITHOUT_THROWS_TAG` rule when it adds a @throws annotation to the function, which has `throw` inside try-catch block - Added tests, using real exceptions Closes #1718 --- .../rules/chapter2/kdoc/KdocMethods.kt | 8 ++++---- .../ruleset/chapter2/KdocMethodsTest.kt | 20 +++++++++---------- .../methods/KdocWithoutThrowsTagExpected.kt | 7 ++++++- .../methods/KdocWithoutThrowsTagTested.kt | 10 +++++++++- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter2/kdoc/KdocMethods.kt b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter2/kdoc/KdocMethods.kt index 62382c4f80..6e23cf516e 100644 --- a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter2/kdoc/KdocMethods.kt +++ b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter2/kdoc/KdocMethods.kt @@ -211,8 +211,8 @@ class KdocMethods(configRules: List) : DiktatRule( if (parent?.elementType == TRY) { val catchNodes = parent.getAllChildrenWithType(CATCH) - val findName = catchNodes.firstOrNull { catchNode -> - val matchingName = catchNode.findAllDescendantsWithSpecificType(IDENTIFIER) + val findNodeWithMatchingName = catchNodes.firstOrNull { catchNode -> + val matchingNameForCatchNode = catchNode.findAllDescendantsWithSpecificType(IDENTIFIER) .firstOrNull { catchNodeName -> nodeName.text == catchNodeName.text || try { val nodeClass = forName("java.lang.${nodeName.text}") @@ -223,9 +223,9 @@ class KdocMethods(configRules: List) : DiktatRule( false } } - matchingName != null + matchingNameForCatchNode != null } - return findName != null + return findNodeWithMatchingName != null } return false } diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/KdocMethodsTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/KdocMethodsTest.kt index 0890a37f24..d9febd8970 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/KdocMethodsTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/KdocMethodsTest.kt @@ -285,12 +285,12 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) { fun foo(a: Int): Int { try { if (a < 0) - throw ex1() - } catch (e: ex1) { + throw NumberFormatExecption() + } catch (e: ArrayIndexOutOfBounds) { print(1) - } catch (e: ex2) { + } catch (e: NullPointerException) { print(2) - } catch (e: ex3) { + } catch (e: NumberFormatExecption) { print(3) } return 2 * a @@ -311,18 +311,18 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) { fun foo(a: Int): Int { try { if (a < 0) - throw ex1() - throw ex2() - throw ex3() - } catch (e: ex1) { + throw NumberFormatException() + throw NullPointerException() + throw NoSuchElementException() + } catch (e: NoSuchElementException) { print(1) - } catch (e: ex2) { + } catch (e: IllegalArgumentException) { print(2) } return 2 * a } """.trimIndent(), - DiktatError(1, 1, ruleId, "${KDOC_WITHOUT_THROWS_TAG.warnText()} foo (ex3)", true)) + DiktatError(1, 1, ruleId, "${KDOC_WITHOUT_THROWS_TAG.warnText()} foo (NullPointerException)", true)) } @Test diff --git a/diktat-rules/src/test/resources/test/paragraph2/kdoc/package/src/main/kotlin/com/saveourtool/diktat/kdoc/methods/KdocWithoutThrowsTagExpected.kt b/diktat-rules/src/test/resources/test/paragraph2/kdoc/package/src/main/kotlin/com/saveourtool/diktat/kdoc/methods/KdocWithoutThrowsTagExpected.kt index fda6f29bcb..3af3dc8699 100644 --- a/diktat-rules/src/test/resources/test/paragraph2/kdoc/package/src/main/kotlin/com/saveourtool/diktat/kdoc/methods/KdocWithoutThrowsTagExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph2/kdoc/package/src/main/kotlin/com/saveourtool/diktat/kdoc/methods/KdocWithoutThrowsTagExpected.kt @@ -3,15 +3,20 @@ package test.paragraph2.kdoc.`package`.src.main.kotlin.com.saveourtool.diktat.kd /** * @param onSuccess * @param onFailure + * @throws ArrayIndexOutOfBounds */ fun parseInputNumber(onSuccess: (number: Int) -> Unit, onFailure: () -> Unit) { try { val input: Int = binding.inputEditText.text.toString().toInt() if (input < 0) throw NumberFormatException() + throw ArrayIndexOutOfBounds() + throw NullPointerException() onSuccess(input) - } catch (e: NumberFormatException) { + } catch (e: IllegalArgumentException) { + onFailure() + } catch (e: NullPointerException) { onFailure() } } diff --git a/diktat-rules/src/test/resources/test/paragraph2/kdoc/package/src/main/kotlin/com/saveourtool/diktat/kdoc/methods/KdocWithoutThrowsTagTested.kt b/diktat-rules/src/test/resources/test/paragraph2/kdoc/package/src/main/kotlin/com/saveourtool/diktat/kdoc/methods/KdocWithoutThrowsTagTested.kt index 8c960770b8..5cd21a66e8 100644 --- a/diktat-rules/src/test/resources/test/paragraph2/kdoc/package/src/main/kotlin/com/saveourtool/diktat/kdoc/methods/KdocWithoutThrowsTagTested.kt +++ b/diktat-rules/src/test/resources/test/paragraph2/kdoc/package/src/main/kotlin/com/saveourtool/diktat/kdoc/methods/KdocWithoutThrowsTagTested.kt @@ -1,13 +1,21 @@ package test.paragraph2.kdoc.`package`.src.main.kotlin.com.saveourtool.diktat.kdoc.methods +/** + * @param onSuccess + * @param onFailure + */ fun parseInputNumber(onSuccess: (number: Int) -> Unit, onFailure: () -> Unit) { try { val input: Int = binding.inputEditText.text.toString().toInt() if (input < 0) throw NumberFormatException() + throw ArrayIndexOutOfBounds() + throw NullPointerException() onSuccess(input) - } catch (e: NumberFormatException) { + } catch (e: IllegalArgumentException) { + onFailure() + } catch (e: NullPointerException) { onFailure() } }