saveourtool / diktat

Strict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
https://diktat.saveourtool.com
MIT License
521 stars 39 forks source link

`KDOC_WITHOUT_THROWS_TAG` add `@throws` annotation when throw inside try catch #1718

Closed benjdero closed 9 months ago

benjdero commented 1 year ago

Describe the bug

I sometime use throw inside a try catch when I feel like it makes sense, for example to do extra verifications on user input (see example below).

Expected behavior

Do nothing

Observed behavior

KDOC_WITHOUT_THROWS_TAG rule add a @throws annotation to the function, but it's wrong, this function does not throw anything as it is already caught by the surrounding try catch

Steps to Reproduce

/**
 * @throws NumberFormatException
 */
fun parseInputNumber(onSuccess: (number: Int) -> Unit, onFailure: () -> Unit) {
    try {
        val input: Int = binding.inputEditText.text.toString().toInt()
        if (input < 0)
            throw NumberFormatException()

        onSuccess(input)
    } catch (e: NumberFormatException) {
        onFailure()
    }
}

Environment information

orchestr7 commented 1 year ago

Hey! thank you for this report! Sounds reasonable, looks like we implemented it in a very-very simple way. Need to think of how to do it properly. Until fixed you can use @Suppress annotation