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
Implement rule UnnecessaryPartOfBinaryExpression
#5203
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4587669
Implement check for unnecessary part of binary expression
7275282
Update the default config
b2be976
Fixed import and code style
d45edf1
Fixed import
c74d866
Fixed ci issues
02b9007
Update detekt-rules-performance/src/main/kotlin/io/gitlab/arturbosch/…
VovaStelmashchuk 6b79951
Fixed tests
VovaStelmashchuk File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...kotlin/io/gitlab/arturbosch/detekt/rules/performance/UnnecessaryPartOfBinaryExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.gitlab.arturbosch.detekt.rules.performance | ||
|
||
import io.gitlab.arturbosch.detekt.api.CodeSmell | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Debt | ||
import io.gitlab.arturbosch.detekt.api.Entity | ||
import io.gitlab.arturbosch.detekt.api.Issue | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import org.jetbrains.kotlin.psi.KtBinaryExpression | ||
|
||
/** | ||
* Unnecessary binary expression add complexity to the code and accomplish nothing. They should be removed. | ||
* The rule works with all binary expression included if and when condition. The rule also works with all predicates. | ||
* | ||
* <noncompliant> | ||
* val foo = true | ||
* | ||
* if (foo || foo) { | ||
* } | ||
* </noncompliant> | ||
* | ||
* <compliant> | ||
* val foo = true | ||
* if (foo) { | ||
* } | ||
* </compliant> | ||
* | ||
*/ | ||
class UnnecessaryPartOfBinaryExpression(config: Config = Config.empty) : Rule(config) { | ||
|
||
override val issue: Issue = Issue( | ||
"UnnecessaryPartOfBinaryExpression", | ||
Severity.Performance, | ||
"Detects duplicate condition into binary expression and recommends to remove unnecessary checks", | ||
Debt.FIVE_MINS | ||
) | ||
|
||
override fun visitBinaryExpression(expression: KtBinaryExpression) { | ||
super.visitBinaryExpression(expression) | ||
val children = expression.children.map { it.text.replace(Regex("\\s"), "") } | ||
|
||
if (children.size != children.distinct().size) { | ||
report(CodeSmell(issue, Entity.from(expression), issue.description)) | ||
} | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
...in/io/gitlab/arturbosch/detekt/rules/performance/UnnecessaryPartOfBinaryExpressionSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package io.gitlab.arturbosch.detekt.rules.performance | ||
|
||
import io.gitlab.arturbosch.detekt.test.compileAndLint | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class UnnecessaryPartOfBinaryExpressionSpec { | ||
|
||
@Test | ||
fun `verify foo or foo detected`() { | ||
val code = """ | ||
fun bar() { | ||
val foo = true | ||
if (foo || foo) { | ||
//TODO | ||
} | ||
} | ||
""" | ||
val findings = UnnecessaryPartOfBinaryExpression().compileAndLint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `verify object-foo or object-foo detected`() { | ||
val code = """ | ||
class Bar(val bar: Boolean) | ||
fun bar() { | ||
val bar = Bar(true) | ||
|
||
if (bar.bar || bar.bar) { | ||
//TODO | ||
} | ||
} | ||
|
||
|
||
""" | ||
val findings = UnnecessaryPartOfBinaryExpression().compileAndLint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `verify foo more 5 and foo more 5 detected`() { | ||
val code = """ | ||
fun bar() { | ||
val foo = 1 | ||
if (foo > 1 && foo > 1) { | ||
//TODO | ||
} | ||
} | ||
""" | ||
val findings = UnnecessaryPartOfBinaryExpression().compileAndLint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `verify foo more 5 && foo more 5 detected un trim`() { | ||
val code = """ | ||
fun bar() { | ||
val foo = 1 | ||
if (foo> 1 && foo >1) { | ||
//TODO | ||
} | ||
} | ||
""" | ||
val findings = UnnecessaryPartOfBinaryExpression().compileAndLint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `verify object-foo && object-foo detected`() { | ||
val code = """ | ||
class Bar(val bar: Boolean) | ||
|
||
fun bar() { | ||
val bar = Bar(true) | ||
|
||
if (bar.bar && bar.bar) { | ||
//TODO | ||
} | ||
} | ||
""" | ||
val findings = UnnecessaryPartOfBinaryExpression().compileAndLint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `verify foo does not report`() { | ||
val code = """ | ||
fun bar() { | ||
val foo = true | ||
if (foo) { | ||
//TODO | ||
} | ||
} | ||
|
||
|
||
""" | ||
val findings = UnnecessaryPartOfBinaryExpression().compileAndLint(code) | ||
assertThat(findings).hasSize(0) | ||
} | ||
|
||
@Test | ||
fun `verify more and less if works as expected`() { | ||
val code = """ | ||
fun bar() { | ||
val foo = 0 | ||
val bar = 1 | ||
if (foo > bar || foo > 1) { | ||
//TODO | ||
} | ||
} | ||
|
||
|
||
""" | ||
val findings = UnnecessaryPartOfBinaryExpression().compileAndLint(code) | ||
assertThat(findings).hasSize(0) | ||
} | ||
|
||
@Test | ||
fun `verify into filter function`() { | ||
val code = """ | ||
fun bar() { | ||
val list = listOf<Int>() | ||
|
||
list.filter { it > 1 || it > 1 } | ||
} | ||
""" | ||
val findings = UnnecessaryPartOfBinaryExpression().compileAndLint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `verify into when`() { | ||
val code = """ | ||
fun bar() { | ||
val foo = true | ||
when { | ||
foo || foo -> { | ||
|
||
} | ||
} | ||
} | ||
""" | ||
val findings = UnnecessaryPartOfBinaryExpression().compileAndLint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Are we missing any potential false positives here?
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.
No. I can see possibility for true negative case. In case when condition has extra brackets. Like a (foo && (foo)). I did not add a test for the case because detekt has a forbidden brackets rule.
We can change the condition, and compare contents or add some preprocessing to the children remove the && and ||.
Do we need this improve ?
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.
Not necessarily. The rule "looked" too easy and I wanted to collect some feedback around it. Let's wait for other maintainers' opinions but it should be good to go 👍