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

Add UnreachableCatchBlock rule #3478

Merged
merged 2 commits into from Feb 17, 2021

Conversation

t-kameyama
Copy link
Contributor

Fixes #305

@codecov
Copy link

codecov bot commented Feb 16, 2021

Codecov Report

Merging #3478 (0cb50f3) into master (623e7fd) will decrease coverage by 0.01%.
The diff coverage is 75.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3478      +/-   ##
============================================
- Coverage     80.28%   80.27%   -0.02%     
- Complexity     2784     2793       +9     
============================================
  Files           454      455       +1     
  Lines          8415     8434      +19     
  Branches       1608     1614       +6     
============================================
+ Hits           6756     6770      +14     
  Misses          789      789              
- Partials        870      875       +5     
Impacted Files Coverage Δ Complexity Δ
...urbosch/detekt/rules/bugs/UnreachableCatchBlock.kt 72.22% <72.22%> (ø) 9.00 <9.00> (?)
...turbosch/detekt/rules/bugs/PotentialBugProvider.kt 100.00% <100.00%> (ø) 3.00 <0.00> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 623e7fd...0cb50f3. Read the comment docs.

/**
* Reports unreachable catch blocks.
*
* <noncompliant>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to add an extended description and compliant code example in order to have a better understand of the rule from the user point of view.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


val tryExpression = catchClause.getStrictParentOfType<KtTryExpression>() ?: return
val prevCatchClauses = tryExpression.catchClauses.takeWhile { it != catchClause }
if (prevCatchClauses.isEmpty()) return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this if empty check even necessary considering the any statement underneath?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid unnecessary catchClassDescriptor() call in the following case.

try {
} catch (e: Exception) {
}

@schalkms
Copy link
Member

I like this rule 👍
Thank you!

Copy link
Member

@BraisGabin BraisGabin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one!

@schalkms schalkms merged commit 740dfe8 into detekt:master Feb 17, 2021
@t-kameyama t-kameyama deleted the UnreachableCatchBlock branch February 17, 2021 21:48
@cortinico cortinico added this to the 1.16.0 milestone Feb 19, 2021
This was referenced Mar 11, 2021
This was referenced Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect unreachable catch blocks
4 participants