Skip to content

Implement rule UnnecessaryPartOfBinaryExpression#5203

Merged
BraisGabin merged 7 commits into
detekt:mainfrom
gr8-toolkit:main
Sep 7, 2022
Merged

Implement rule UnnecessaryPartOfBinaryExpression#5203
BraisGabin merged 7 commits into
detekt:mainfrom
gr8-toolkit:main

Conversation

@VovaStelmashchuk

@VovaStelmashchuk VovaStelmashchuk commented Aug 8, 2022

Copy link
Copy Markdown
Contributor

Implement the UnnecessaryPartOfBinaryExpression. The rule works with all binary expression include if and when condition. The rule also works with all predicates.

@github-actions github-actions Bot added the rules label Aug 8, 2022
@github-actions

github-actions Bot commented Aug 8, 2022

Copy link
Copy Markdown
Warnings
⚠️ This PR is approved with no milestone set. If merged, it won't appear in the Detekt release notes.
Messages
📖 Thanks for adding a new rule to Detekt ❤️

Generated by 🚫 dangerJS against 6b79951

@cortinico cortinico changed the title Implement check for unnecessary part of binary expression Implement rule UnnecessaryPartOfBinaryExpression Aug 8, 2022
super.visitBinaryExpression(expression)
val children = expression.children.map { it.text.replace(Regex("\\s"), "") }

if (children.size != children.distinct().size) {

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.

Are we missing any potential false positives here?

Copy link
Copy Markdown
Contributor Author

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 ?

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.

Do we need this improve ?

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 👍

…detekt/rules/performance/UnnecessaryPartOfBinaryExpression.kt

Co-authored-by: Nicola Corti <corti.nico@gmail.com>
@marschwar

Copy link
Copy Markdown
Contributor

Don't get me wrong, but is this really a problem in die wild that needs its own rule?

I know this is not a binary expression but this is essentially the same thing and not detected by the rule.

fun bar() {
    val foo = true
    val baz = false
    if (foo || baz || foo) {
        //TODO    
    }
}

I know that it does not make much sense but an xor would also be flagged but removing the second part would not yield the same result.

@VovaStelmashchuk

Copy link
Copy Markdown
Contributor Author

@marschwar The rule will report an issue in the line if (foo || baz || foo) {

@marschwar

Copy link
Copy Markdown
Contributor

@marschwar The rule will report an issue in the line if (foo || baz || foo) {

I tried and it does not.

@VovaStelmashchuk

Copy link
Copy Markdown
Contributor Author

Ok, I added the test case, and fix them.

@VovaStelmashchuk

Copy link
Copy Markdown
Contributor Author

@marschwar I added your test case and several new tests and rewrite the rule.

@cortinico cortinico added this to the 1.22.0 milestone Aug 18, 2022
@BraisGabin

Copy link
Copy Markdown
Member

Don't get me wrong, but is this really a problem in die wild that needs its own rule?

I have same the impression... Does this rule "borns" from a real problem in your code base? Could you explain your use case for this rule?

@VovaStelmashchuk

Copy link
Copy Markdown
Contributor Author

Don't get me wrong, but is this really a problem in die wild that needs its own rule?

I have same the impression... Does this rule "borns" from a real problem in your code base? Could you explain your use case for this rule?

Yes, I started a rule development because I detect something like if (foo || foo) into own code base.

@cortinico

Copy link
Copy Markdown
Member

@BraisGabin are you fine merging this?

@BraisGabin

BraisGabin commented Sep 7, 2022

Copy link
Copy Markdown
Member

Yes, I started a rule development because I detect something like if (foo || foo) into own code base.

Thanks!

@BraisGabin are you fine merging this?

Yes, I'm :)

@BraisGabin BraisGabin merged commit c1178f2 into detekt:main Sep 7, 2022
@VovaStelmashchuk

Copy link
Copy Markdown
Contributor Author

The check is red after merge the pull request. It happened because the merge request pipeline executed before new rules applied to the repository.

Do I need to create a new MR for fix the issues ?

Is the currenty pipline for pull request correct ? (Looks like the repo need one more check [is branch are up to date with main])

@BraisGabin

Copy link
Copy Markdown
Member

Yes, please, could you create a PR solving that?

And about a new check... Yes, we could have it but I think that we should rethink our pipelines in general, we have far too much. They are overwhelming to me so they could be even scary for a new contributor.

@VovaStelmashchuk

Copy link
Copy Markdown
Contributor Author

will be done today

@cortinico

Copy link
Copy Markdown
Member

(Looks like the repo need one more check [is branch are up to date with main])

This can be enforced at the Github level (inside the branch protection rule). It would create a lot of friction though as it will require every PR to be rebased before merging.

I'm fine with some failures like this one from time to time as the number of PR we receive is not that high to require extra tooling (like merge queues or other checks).

@VovaStelmashchuk

Copy link
Copy Markdown
Contributor Author

@BraisGabin fixed #5280

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.

4 participants