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

UnreachableCatchBlock gives false positive for unresolved classes #6626

Closed
thejk opened this issue Nov 13, 2023 · 7 comments
Closed

UnreachableCatchBlock gives false positive for unresolved classes #6626

thejk opened this issue Nov 13, 2023 · 7 comments

Comments

@thejk
Copy link

thejk commented Nov 13, 2023

Expected Behavior

No UnreachableCatchBlock warnings should be triggered for classes that could for some reason not be resolved.

Observed Behavior

UnreachableCatchBlock treats each unresolved class as a subclass of each other triggering warnings.

Steps to Reproduce

fun test() {
    try {
    } catch (e: Foo) {
    } catch (e: Bar) {
    }
}

Running detekt with type resolution enabled but Foo and Bar missing from classpath.
detekt-cli -cp . -i test.kt
Output (I've removed other warnings for clarity):

test.kt:10:7: This catch block is unreachable. [UnreachableCatchBlock]

Context

Getting the classpath for type resolution correct for detekt is not easy and getting unrelated warnings because a type doesn't resolve isn't helping.

Your Environment

  • Version of detekt used: 1.23.3
  • Operating System and version: Lnux 6.1.61 x86_64
@thejk thejk added the bug label Nov 13, 2023
thejk added a commit to thejk/detekt that referenced this issue Nov 13, 2023
Kotlin compiler uses ErrorClassDescriptor for classes that doesn't
resolve correctly. They all are subclasses of each other causing
the check in UnreachableCatchBlock to consider all them unreachable.

Ignore ErrorClassDescriptor's to not add most likely false warnings
as we can't know if the classes are subclasses or not.

Fixes detekt#6626
@josephlbarnett
Copy link
Contributor

josephlbarnett commented Nov 15, 2023

same as/related to #6129 ?

@thejk
Copy link
Author

thejk commented Nov 15, 2023

Might be the same base issue (ClassDescriptor for unresolved classes not useful) but no overlap with Kotlin's unreachable code detection in UnreachableCatchBlock I think.

@3flex
Copy link
Member

3flex commented Nov 16, 2023

Getting the classpath for type resolution correct for detekt is not easy

This is the actual issue that we'd want to fix. Is there a specific problem you're facing in passing the right classpath? That might inspire PRs to improve the situation.

@thejk
Copy link
Author

thejk commented Nov 16, 2023

I'm still looking at why the types are not resolved in the real project, but with more than 100 dependencies and their transitive dependencies it's hard to nail down.

That said, should detekt show errors for unresolved types then? Because the only output right now is an incorrect warning about UnreachableCatchBlock which I think is unhelpful (I had to read the rule source code to figure out why it was warning about two unrelated exception classes).

@thejk
Copy link
Author

thejk commented Nov 21, 2023

Found the reason for the missing classes, an Android project and the bootclasspath was not included (so java.lang.Object was missing).

So, for me, a more visible warning about missing classes would make false positives caused by the missing classes less confusing. Not showing the false positives for missing classes would also help.

thejk added a commit to thejk/detekt that referenced this issue Nov 23, 2023
Kotlin compiler uses ErrorClassDescriptor for classes that doesn't
resolve correctly. They all are subclasses of each other causing
the check in UnreachableCatchBlock to consider all them unreachable.

Ignore ErrorClassDescriptor's to not add most likely false warnings
as we can't know if the classes are subclasses or not.

Fixes detekt#6626
@BraisGabin
Copy link
Member

So, for me, a more visible warning about missing classes would make false positives caused by the missing classes less confusing. Not showing the false positives for missing classes would also help.

This is a kind of controversial topic and we had different ideas reggarding this. At the begginig we were REALLY verbose. Fro that reason we added #4423. But even that was too noisy for some people so we end up with #5449.

The problem is that there is too difficult to configure the classpath correctly and on Android it is kind of impossoble so those messages doesn't help too much.

If you run detekt on debug mode you will see all the issues.

@thejk
Copy link
Author

thejk commented Nov 28, 2023

I see, I would have suggested something like number reporting in #4423, but fair enough, until it's possible to always get it to zero it's not good. Just have to get used to set debug = true as soon as I get any weird reports.

I'm resolving this as I believe there are enough issues about this, thanks for the info.

@thejk thejk closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants