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 6e23cf516e..c5b69d0a64 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 @@ -12,6 +12,7 @@ import com.saveourtool.diktat.ruleset.utils.KotlinParser import com.saveourtool.diktat.ruleset.utils.appendNewlineMergingWhiteSpace import com.saveourtool.diktat.ruleset.utils.findAllDescendantsWithSpecificType import com.saveourtool.diktat.ruleset.utils.findChildAfter +import com.saveourtool.diktat.ruleset.utils.findParentNodeWithSpecificType import com.saveourtool.diktat.ruleset.utils.getAllChildrenWithType import com.saveourtool.diktat.ruleset.utils.getBodyLines import com.saveourtool.diktat.ruleset.utils.getFilePath @@ -203,16 +204,13 @@ class KdocMethods(configRules: List) : DiktatRule( private fun isThrowInTryCatchBlock(node: ASTNode?): Boolean { node ?: return false - var parent = node.treeParent - while (parent != null && parent.elementType != TRY) { - parent = parent.treeParent - } + val parent = node.findParentNodeWithSpecificType(TRY) val nodeName = node.findAllDescendantsWithSpecificType(IDENTIFIER).firstOrNull() ?: return false if (parent?.elementType == TRY) { - val catchNodes = parent.getAllChildrenWithType(CATCH) - val findNodeWithMatchingName = catchNodes.firstOrNull { catchNode -> - val matchingNameForCatchNode = catchNode.findAllDescendantsWithSpecificType(IDENTIFIER) + val catchNodes = parent?.getAllChildrenWithType(CATCH) + val findNodeWithMatchingCatch = catchNodes?.firstOrNull { catchNode -> + val matchingNodeForCatchNode = catchNode.findAllDescendantsWithSpecificType(IDENTIFIER) .firstOrNull { catchNodeName -> nodeName.text == catchNodeName.text || try { val nodeClass = forName("java.lang.${nodeName.text}") @@ -223,9 +221,9 @@ class KdocMethods(configRules: List) : DiktatRule( false } } - matchingNameForCatchNode != null + matchingNodeForCatchNode != null } - return findNodeWithMatchingName != null + return findNodeWithMatchingCatch != null } return false } diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/KdocMethodsFixTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/KdocMethodsFixTest.kt index c1a6ead494..93fa42bc35 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/KdocMethodsFixTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/KdocMethodsFixTest.kt @@ -64,7 +64,7 @@ class KdocMethodsFixTest : FixTestBase("test/paragraph2/kdoc/package/src/main/ko @Test @Tag(WarningNames.KDOC_WITHOUT_THROWS_TAG) - fun `@throws tag shouldn't be added inside try-catch`() { + fun `Should add throws tag only for throw without catch`() { fixAndCompare("KdocWithoutThrowsTagExpected.kt", "KdocWithoutThrowsTagTested.kt") } } 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 d9febd8970..0908afd7ee 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 @@ -274,7 +274,7 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) { @Test @Tag(WarningNames.KDOC_WITHOUT_THROWS_TAG) - fun `No warning when catch without matching throw`() { + fun `No warning when throw has matching catch`() { lintMethod( """ /**