Skip to content

UnnecessaryLet: fix false positive when let is used for destructuring - #2968

Merged
schalkms merged 4 commits into
detekt:masterfrom
t-kameyama:issue_2966
Aug 15, 2020
Merged

schalkms merged 4 commits into
detekt:masterfrom
t-kameyama:issue_2966

Conversation

@t-kameyama

Copy link
Copy Markdown
Contributor

Fixes #2966

@codecov

codecov Bot commented Aug 14, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #2968 into master will increase coverage by 0.03%.
The diff coverage is 76.59%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2968      +/-   ##
============================================
+ Coverage     80.25%   80.29%   +0.03%     
- Complexity     2465     2478      +13     
============================================
  Files           423      424       +1     
  Lines          7440     7479      +39     
  Branches       1358     1370      +12     
============================================
+ Hits           5971     6005      +34     
+ Misses          763      760       -3     
- Partials        706      714       +8     
Impacted Files Coverage Δ Complexity Δ
...turbosch/detekt/rules/bugs/NullableToStringCall.kt 74.19% <74.19%> (ø) 10.00 <10.00> (?)
...ab/arturbosch/detekt/rules/style/UnnecessaryLet.kt 82.22% <76.92%> (+2.73%) 13.00 <0.00> (ø)
...turbosch/detekt/rules/bugs/PotentialBugProvider.kt 100.00% <100.00%> (ø) 2.00 <0.00> (ø)
.../arturbosch/detekt/rules/style/UnnecessaryApply.kt 88.46% <100.00%> (+0.46%) 8.00 <0.00> (+1.00)
.../gitlab/arturbosch/detekt/api/internal/BaseRule.kt 100.00% <0.00%> (+6.25%) 6.00% <0.00%> (ø%)
...rturbosch/detekt/api/internal/CompilerResources.kt 100.00% <0.00%> (+66.66%) 3.00% <0.00%> (+2.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 d5ecca3...989744d. Read the comment docs.

@BraisGabin BraisGabin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👏👏👏

return bodyExpression?.countVarRefs(firstParameter?.text ?: IT_LITERAL)
private fun KtLambdaExpression.countReferences(): Int {
val bodyExpression = bodyExpression ?: return 0
val firstParameter = firstParameter

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this one can be removed safely.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

val firstParameter = firstParameter

@schalkms

Copy link
Copy Markdown
Member

👏

@cortinico cortinico left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the contribution 🙏

val bodyExpression = bodyExpression ?: return 0
val firstParameter = firstParameter
val destructuringDeclaration = firstParameter?.destructuringDeclaration
return destructuringDeclaration?.entries?.sumBy { bodyExpression.countVarRefs(it.nameAsSafeName.asString()) }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you divide this expression in 2? Having multiple occurrencies of the elvis operator on the same line makes the code harder to read.

@schalkms
schalkms merged commit c2ea103 into detekt:master Aug 15, 2020
@t-kameyama
t-kameyama deleted the issue_2966 branch August 15, 2020 20:20
@arturbosch arturbosch added this to the 1.11.1 milestone Aug 16, 2020
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.

UnnecessaryLet false positive when let is used for destructuring

5 participants