Add UnusedUnaryOperator rule#3499
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3499 +/- ##
============================================
- Coverage 77.63% 77.58% -0.05%
- Complexity 2814 2822 +8
============================================
Files 462 463 +1
Lines 8709 8732 +23
Branches 1688 1697 +9
============================================
+ Hits 6761 6775 +14
- Misses 1036 1037 +1
- Partials 912 920 +8
Continue to review full report at Codecov.
|
| operator fun Foo.unaryMinus() = Foo(-x) | ||
| fun test() { | ||
| val p = Foo(1) + Foo(2) | ||
| - Foo(3) |
There was a problem hiding this comment.
This case could have false negatives, but that's better than false postives for sure.
|
|
||
| if (expression.baseExpression == null) return | ||
| val operationToken = expression.operationToken | ||
| if (operationToken != KtTokens.PLUS && operationToken != KtTokens.MINUS) return |
There was a problem hiding this comment.
Is there a reason why .not() (the !) unary operator is not considered in this rule?
There was a problem hiding this comment.
! cannot be used as a binary operator.
...errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/UnusedUnaryOperatorSpec.kt
Outdated
Show resolved
Hide resolved
…etekt/rules/bugs/UnusedUnaryOperatorSpec.kt Co-authored-by: Nicola Corti <corti.nico@gmail.com>
|
I talked with my colleagus about this rule and found out some interesting discussions:
With some manual testing would fail because of But even we change it to still failed because of I believe this new rule is already covered by the combination of |
|
ktlint reports nothing in the following case: fun test() {
val x = 1 + 2
+3
} |
In this scenario, |
|
I think this problem is easier to understand with one detekt rule than with a combination of two ktlint rules, but if you don't need this rule, close this PR. |
|
@cortinico @BraisGabin What do you guys think? If both of you support this rule, I am fine with merging this in. |
I don't have a strong opinion honestly. I would lean towards merging this. Mostly because is disabled by default and because explicit is better than implicit, we offer a rule that covers this exact scenario rather than "suggesting" a combination of two. |
|
I think that this is a code smell problem, not a formatting problem. So I vote to merge it. It would be nice to have "experimental rules". Rules that we are not sure if we want to keep or not because they can have far too much false positive or they could be too subjective... But I don't think that this rule should wait for us to implement something like that. |
picklebento
left a comment
There was a problem hiding this comment.
Thank you for the great test coverage. I played around with the code a little bit and found that those test cases were quite exhaustive for false positives.
Fixes #312