Skip to content

Commit

Permalink
### What's done:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
diphtongue committed Dec 14, 2023
1 parent 7aad856 commit 23fd0cf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ class KdocMethods(configRules: List<RulesConfig>) : 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}")
Expand All @@ -223,9 +223,9 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(
false
}
}
matchingName != null
matchingNameForCatchNode != null
}
return findName != null
return findNodeWithMatchingName != null
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 23fd0cf

Please sign in to comment.