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

Fix UnnecessaryLet false positive in inner lambdas #3841

Merged
merged 2 commits into from
Jun 30, 2021
Merged

Fix UnnecessaryLet false positive in inner lambdas #3841

merged 2 commits into from
Jun 30, 2021

Conversation

xouabita
Copy link
Contributor

When upgrading detekt to 1.7.1, I noticed some false positives for UnnecessaryLet.

For example:

fun callMe(callback: () -> Unit) {
    callback()
}
 
fun test(value: Int?) {
    value?.let { 
        callMe {
            println(it)
        }
     }
}

would be reported.

This PR should fix the issue

@codecov
Copy link

codecov bot commented May 26, 2021

Codecov Report

Merging #3841 (b4cdcde) into main (a3df280) will decrease coverage by 0.00%.
The diff coverage is 70.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #3841      +/-   ##
============================================
- Coverage     83.58%   83.57%   -0.01%     
  Complexity     3110     3110              
============================================
  Files           456      456              
  Lines          8931     8932       +1     
  Branches       1748     1747       -1     
============================================
  Hits           7465     7465              
  Misses          551      551              
- Partials        915      916       +1     
Impacted Files Coverage Δ
...ab/arturbosch/detekt/rules/style/UnnecessaryLet.kt 80.00% <66.66%> (-2.36%) ⬇️
...tlab/arturbosch/detekt/rules/KtLambdaExpression.kt 66.66% <75.00%> (+9.52%) ⬆️

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 a3df280...b4cdcde. Read the comment docs.

@xouabita xouabita marked this pull request as ready for review May 26, 2021 18:52
@BraisGabin
Copy link
Member

That doesn't seem as a false positive. It could be replaced with an if.

@xouabita
Copy link
Contributor Author

@BraisGabin from the docs

a?.let { print(it) }

is compliant

@xouabita
Copy link
Contributor Author

xouabita commented May 26, 2021

also,

fun callMe(callback: () -> Unit) {
    callback()
}
fun test(value: Int?) {
    value?.let { 
        callMe {
            it.plus(it)
        }
     }
}

would also be reported, which is a false positive

@BraisGabin
Copy link
Member

You are right. For me that's an unnecessary usage but this rule, right now, should not flag it.

@cortinico cortinico added this to the 1.18.0 milestone May 27, 2021
@xouabita
Copy link
Contributor Author

Hi, thank you very much for reviewing this PR. Is there anything else I need to do before this PR can be merged ?

@BraisGabin
Copy link
Member

I don't think so. Sorry for the late merge and thanks for the fix :)

@BraisGabin BraisGabin merged commit f6d75dd into detekt:main Jun 30, 2021
@xouabita xouabita deleted the fix-unnecessary-let branch June 30, 2021 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants