Skip to content
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

Merged
merged 3 commits into from
Jan 30, 2021

Conversation

cortinico
Copy link
Member

@cortinico cortinico commented Jan 27, 2021

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

val list : List<Int> = getAList()
if (list is MutableList) {
    list.add(42)
}

(list as MutableList).add(42)

Compliant

val list : List<Int> = getAList()
list.toMutableList().add(42)

Happy to discuss if we want to adapt it to check casts against Java concrete collection types (e.g. ArrayList, etc.).

@codecov
Copy link

codecov bot commented Jan 27, 2021

Codecov Report

Merging #3413 (a20691d) into master (2730336) will decrease coverage by 0.00%.
The diff coverage is 81.81%.

Impacted file tree graph

@@             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     
Impacted Files Coverage Δ Complexity Δ
...h/detekt/rules/bugs/DontDowncastCollectionTypes.kt 81.25% <81.25%> (ø) 8.00 <8.00> (?)
...turbosch/detekt/rules/bugs/PotentialBugProvider.kt 100.00% <100.00%> (ø) 3.00 <0.00> (ø)
...ain/kotlin/io/github/detekt/metrics/LinesOfCode.kt 92.30% <0.00%> (-0.80%) 0.00% <0.00%> (ø%)
...gitlab/arturbosch/detekt/cli/runners/AstPrinter.kt 100.00% <0.00%> (ø) 4.00% <0.00%> (ø%)
...turbosch/detekt/rules/style/UnusedPrivateMember.kt 90.76% <0.00%> (+0.14%) 5.00% <0.00%> (ø%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2730336...a20691d. Read the comment docs.

@cortinico
Copy link
Member Author

This rule is inspired from Effective Kotlin: Item 1 - Limit Mutability (credits to @MarcinMoskala)

@BraisGabin
Copy link
Member

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.

Copy link
Member

@schalkms schalkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great rule!

@cortinico
Copy link
Member Author

Great rule! I think that it would be good to think of ArrayList at least because it exists in kotlin (with a type alias).

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

@cortinico cortinico merged commit c5fc670 into detekt:master Jan 30, 2021
@cortinico cortinico deleted the nc/rule-dont-downcast branch January 30, 2021 19:40
@cortinico cortinico added this to the 1.16.0 milestone Feb 19, 2021
This was referenced Mar 11, 2021
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.

None yet

3 participants