-
-
Notifications
You must be signed in to change notification settings - Fork 766
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 negatives #2828
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2828 +/- ##
============================================
+ Coverage 80.62% 80.65% +0.02%
+ Complexity 2350 2346 -4
============================================
Files 388 388
Lines 7025 7039 +14
Branches 1282 1288 +6
============================================
+ Hits 5664 5677 +13
Misses 717 717
- Partials 644 645 +1
Continue to review full report at Codecov.
|
😭 I never understand codecov. I did improved the test coverage here. Is it complainly for moving a function from one file to another? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 good job
Great job!
The script is located at In case you are not familiar with Groovy: |
All this examples are flagged by the new rule. I need your opinion if they are valid, false positives or they are edge cases where a
lineNumberColumnLength =
(node.lastChildLeafOrSelf().lineNumber() ?: 1)
.let { var v = it; var c = 0; while (v > 0) { c++; v /= 10 }; c }
val rightBraceIndent = node.treeParent
.prevLeaf { it is PsiWhiteSpace && it.textContains('\n') }?.text.orEmpty()
.let { "\n${it.substringAfterLast("\n")}" }
while (true) {
val dt = time.let { old -> awaitFrame().also { time = it } - old }
if (dt > 0.5e9) continue // don't animate through over a half second lapses
Unirest.get("${http.origin}/html.html").basicAuth("u", "p").asString().let { assertThat(it.body).contains("HTML works") }
ruleSetId?.let { acceptedSuppressionIds.addAll(listOf(ruleSetId, "$ruleSetId.$id", "$ruleSetId:$id")) }
fun KClass<*>.shouldHaveFunction(name: String, block: (KFunction<*>) -> Unit) {
this should haveFunction(name)
findFunction(name)?.let(block)
} I think that all of them can be ommited and will improve the code. Some of them just removing the let and others moving code to a function. But in any case I think that let is a good fit there. The only case where I'm not that sure is the 6th. Should |
Thanks for testing this new rule thoroughly. fun KClass<*>.shouldHaveFunction(name: String, block: (KFunction<*>) -> Unit) {
this should haveFunction(name)
findFunction(name)?.let(block)
} |
That code is the same as this one: fun KClass<*>.shouldHaveFunction(name: String, block: (KFunction<*>) -> Unit) {
this should haveFunction(name)
findFunction(name)?.let { block(it) }
} And this is legit. So I imagine that we should allow every use of I'll add test for this two cases and fix them 👍. |
Ok, false positive fixed. Let me know what do you think now :). It's ready to merge for me. |
report(CodeSmell(issue, Entity.from(expression), "let expression can be omitted")) | ||
} | ||
} else { | ||
val count = lambdaExpr.countReferences() ?: 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename this to lambdaReferenceCount to make it more readable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Closes #2826
There were some false negatives in this rule:
a.let { 1.plus(1) } // 1.plus(1)
a?.let { 1.plus(1) } // if (a != null) 1.plus(1)
This one is to avoid overuse of let when you get nothing from it.a.let { println(it) } // println(it)
a.let { println(it); println("hola") }
I know, the tests are a real mess. I'll try to refactor them.
I know that we have a script/project to run detekt over different projects to ensure that the new rule works... Where is it? I'll like to prove this on real projects.