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: DoubleMutabilityForCollection #3553

Merged
merged 2 commits into from
Apr 10, 2021

Conversation

cortinico
Copy link
Member

I'm adding a new rule that detects if you're declaring a var with also a Mutable type like:

var myList = mutableListOf(1,2,3)

Instead you should prefer to:

val myList = mutableListOf(1,2,3)
// OR
var myList = listOf(1,2,3)

Having "double mutability" is considered an anti-pattern as the myList variable can both be reassigned and .clear()-ed.

@codecov
Copy link

codecov bot commented Mar 14, 2021

Codecov Report

Merging #3553 (fa98b54) into master (b5aac52) will increase coverage by 0.03%.
The diff coverage is 88.88%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3553      +/-   ##
============================================
+ Coverage     77.53%   77.56%   +0.03%     
- Complexity     2829     2833       +4     
============================================
  Files           464      465       +1     
  Lines          8769     8795      +26     
  Branches       1713     1716       +3     
============================================
+ Hits           6799     6822      +23     
  Misses         1046     1046              
- Partials        924      927       +3     
Impacted Files Coverage Δ Complexity Δ
...detekt/rules/bugs/DoubleMutabilityForCollection.kt 88.00% <88.00%> (ø) 4.00 <4.00> (?)
...n/io/github/detekt/report/sarif/RuleDescriptors.kt 20.00% <100.00%> (ø) 0.00 <0.00> (ø)
...turbosch/detekt/rules/bugs/PotentialBugProvider.kt 100.00% <100.00%> (ø) 3.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 b5aac52...684535b. Read the comment docs.

Copy link
Member

@BraisGabin BraisGabin left a comment

Choose a reason for hiding this comment

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

👍

Once we have a real support for severity I'd like to add a rule to detect all the usages of var. There are very few cases where it uses make sense.

@chao2zhang chao2zhang added this to the 1.17.0 milestone Mar 15, 2021
Base automatically changed from master to main March 21, 2021 18:44
Copy link
Member

@chao2zhang chao2zhang left a comment

Choose a reason for hiding this comment

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

Let's keep in mind that we need to annotate this rule later with the new @RequiresTypeResolution #3579

@BraisGabin
Copy link
Member

Let's keep in mind that we need to annotate this rule later with the new @RequiresTypeResolution #3579

#3579 is merged already so I think that we should do this change and we can merge this. The js problem can be faced at #3613

@chao2zhang chao2zhang merged commit be5bfc6 into detekt:main Apr 10, 2021
chao2zhang pushed a commit to chao2zhang/detekt that referenced this pull request May 13, 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

5 participants