Skip to content

Commit

Permalink
Closes #1718
Browse files Browse the repository at this point in the history
  • Loading branch information
diphtongue committed Dec 15, 2023
1 parent 23fd0cf commit ef55ae7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -203,16 +204,13 @@ class KdocMethods(configRules: List<RulesConfig>) : 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}")
Expand All @@ -223,9 +221,9 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(
false
}
}
matchingNameForCatchNode != null
matchingNodeForCatchNode != null
}
return findNodeWithMatchingName != null
return findNodeWithMatchingCatch != null
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
/**
Expand Down

0 comments on commit ef55ae7

Please sign in to comment.