Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
benjdero opened this issue Jul 27, 2023 · 1 comment · Fixed by #1862
Closed

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

benjdero opened this issue Jul 27, 2023 · 1 comment · Fixed by #1862
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@benjdero
Copy link

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

  • diktat version: 1.2.5
  • build tool (maven/gradle): Gradle 8.2.1 & AGP 8.1.0
  • how is diktat run (CLI, plugin, etc.): Plugin
  • kotlin version: 1.9.0
  • operating system: Ubuntu 22.04
  • link to a project (if your project is public): -
@benjdero benjdero added the bug Something isn't working label Jul 27, 2023
@orchestr7
Copy link
Member

orchestr7 commented Jul 27, 2023

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

@0x6675636b796f75676974687562 0x6675636b796f75676974687562 changed the title KDOC_WITHOUT_THROWS_TAG add @throws annotation when throw inside try catch KDOC_WITHOUT_THROWS_TAG add @throws annotation when throw inside try catch Sep 6, 2023
diphtongue added a commit that referenced this issue Dec 13, 2023
- Fixed `KDOC_WITHOUT_THROWS_TAG` rule when it adds a @throws annotation to the function, which has `throw` inside try-catch block

Closes #1718
diphtongue added a commit that referenced this issue Dec 14, 2023
- Fixed `KDOC_WITHOUT_THROWS_TAG` rule when it adds a @throws annotation to the function, which has `throw` inside try-catch block

Closes #1718
diphtongue added a commit that referenced this issue Dec 14, 2023
- 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
diphtongue added a commit that referenced this issue Dec 15, 2023
diphtongue added a commit that referenced this issue Dec 15, 2023
…try-catch (#1862)

### 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants