-
-
Notifications
You must be signed in to change notification settings - Fork 768
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
Add ElseCaseInsteadOfExhaustiveWhen rule #4632
Conversation
I have a question: Should this rule work for |
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.
I'm not inpired right now to provide better descriptions and messages but I think that they should be reformulated following the style guide: https://github.com/detekt/detekt/blob/main/.github/CONTRIBUTING.md#rule-descriptions
* fun whenOnEnumFail(c: Color) { | ||
* when(c) { | ||
* Color.BLUE -> {} | ||
* Color.GREEN -> {} | ||
* else -> {} | ||
* } | ||
* } |
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.
* fun whenOnEnumFail(c: Color) { | |
* when(c) { | |
* Color.BLUE -> {} | |
* Color.GREEN -> {} | |
* else -> {} | |
* } | |
* } | |
* when(c) { | |
* Color.BLUE -> {} | |
* Color.GREEN -> {} | |
* else -> {} | |
* } |
What is it better? To keep the code "compilable" or more "focused" on the issue.
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.
Yeah, I think it improves readability and everyone should get the idea behind this sample. I changed it to your suggested version.
01ea611
to
922d2c5
Compare
922d2c5
to
09a04ef
Compare
@BraisGabin I forced pushed my initial commit again, to make sure I use GitHubs verified commits. I guess you have to approve the pr workflow again.
I think this would make sense. Because changig the following code would be covered by the -fun whenOnBooleanFail(b: Boolean) {
+fun whenOnBooleanFail(b: Boolean?) {
when (b) {
true -> {}
else -> {}
}
} I added the check for boolean types in b93f631. But maybe there is a second opinion. We could also make the behavior about |
Codecov Report
@@ Coverage Diff @@
## main #4632 +/- ##
============================================
+ Coverage 84.52% 84.55% +0.02%
- Complexity 3358 3397 +39
============================================
Files 481 484 +3
Lines 11193 11270 +77
Branches 2042 2059 +17
============================================
+ Hits 9461 9529 +68
Misses 698 698
- Partials 1034 1043 +9
Continue to review full report at Codecov.
|
If Boolean is included (even with a configuration) I would vote to rename the rule to |
I agree. This name fits better and makes it more clear for consumers what this rule does. Changed it. |
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.
The descriptions are perfect now! Thank you so much for this rule :)
else -> emptyList() | ||
KDocKnownTag.AUTHOR, KDocKnownTag.THROWS, KDocKnownTag.EXCEPTION, KDocKnownTag.RECEIVER, | ||
KDocKnownTag.RETURN, KDocKnownTag.SEE, KDocKnownTag.SINCE, KDocKnownTag.CONSTRUCTOR, KDocKnownTag.SAMPLE, | ||
KDocKnownTag.SUPPRESS -> emptyList() |
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.
I think that this case is a good place to add a @Suppress("ElseCaseInsteadOfExhaustiveWhen")
.
This rule is good in general (for example it have a lot of sense the change in AnalysisResult
) but I think that in this case the else
have sense.
I am quite happy with current state of this PR. Only thing that bothers me is the behaviour when used in an Kotlin multiplattform project because of this:
Is there any way for us to detect these cases, or is detekt currently even properly supporting Kotlin multiplatform? |
We are working to support Kotlin multiplatform. But, right now, there are some missing pieces. We can merge this and then open an issue to check what can be done in terms of Kotlin Multiplatform. |
As this is known in the PSI regardless of your target platform/project, you can do so in the following way:
Please update the PR with tests for this case as well 👍 Other than that looks good to me :) |
Updated the PR. Thanks for your feedback everyone. |
Hey @cortinico could you add this rule to the "new rules" section in the 1.20.0 changelog? So there are currently actually 16 new rules in this new minor release. 😄 |
* Add ElseCaseInEnumOrSealedWhen rule * Adapt to new rule by replacing else cases * Change rule name, support booleans, improve docs * Change variable name of boolean condition * Rename rule to ElseCaseInsteadOfExhaustiveWhen * Improve rule descriptions * Suppress rule for outdated documentation method * Not report sealed class subjects that have an expect modifier * Fix sample code syntax and not compile mulitplatform code
Kind of a late answer but just wanted to point out that it looks like in Kotlin lingo (see: https://kotlinlang.org/docs/whatsnew16.html#stable-exhaustive-when-statements-for-enum-sealed-and-boolean-subjects ) an "exhaustive when" is defined as:
So perhaps to avoid confusion this rule should have been named |
I added a rule to flag
when
expressions that have aenum
orsealed
class subject and contain anelse
case.We try to avoid this in our code base, since this leads to automatically handled cases when you add a new enum / sealed class type to existing ones.
I am very open to suggestions regarding the naming and/or description of this rule. 😊
I also started a discussion about this here: #4619