-
-
Notifications
You must be signed in to change notification settings - Fork 759
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
New rule: DontDowncastCollectionTypes #3413
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3413 +/- ##
============================================
- Coverage 80.38% 80.38% -0.01%
- Complexity 2748 2756 +8
============================================
Files 449 450 +1
Lines 8300 8330 +30
Branches 1587 1594 +7
============================================
+ Hits 6672 6696 +24
Misses 774 774
- Partials 854 860 +6
Continue to review full report at Codecov.
|
This rule is inspired from Effective Kotlin: Item 1 - Limit Mutability (credits to @MarcinMoskala) |
Great rule! I think that it would be good to think of ArrayList at least because it exists in kotlin (with a type alias). And I'm not sure about the name. But I can't think of anyone better. |
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.
Great rule!
I've added those 5 type aliases to the mutable types to consider: https://github.com/JetBrains/kotlin/blob/46b7a774b558001c136be225cf4367fa09ba1aee/libraries/stdlib/jvm/src/kotlin/collections/TypeAliases.kt#L13-L17 |
I'm proposing a new rule:
DontDowncastCollectionTypes
Down-casting immutable types from kotlin.collections should be discouraged.
The result of the downcast is platform specific and can lead to unexpected crashes.
Prefer to use instead the
toMutable<Type>()
functions.Non-compliant
Compliant
Happy to discuss if we want to adapt it to check casts against Java concrete collection types (e.g.
ArrayList
, etc.).