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: ReplaceSafeCallChainWithRun #3089

Merged
merged 5 commits into from
Sep 26, 2020
Merged

Conversation

3flex
Copy link
Member

@3flex 3flex commented Sep 19, 2020

I saw this neat trick while browsing Kotlin compiler source.

Instead of having a long chain of safe calls, when many of those safe calls are redundant (because the type returned by the preceding call is non-nullable), that chain of safe calls can be surrounded with run {} and the redundant safe calls can be removed.

This rule only checks from the end of a chain and works backwards, so it won't recommend inserting run blocks in the middle of a safe call chain which I think could be very confusing to read, especially since it could mean multiple run blocks in a very long chain.

The rule also checks for any opportunity to replace a chain by surrounding with run, so any redundant safe calls at the end of a chain will be reported, even if there's only one. I personally think this is fine because it still reduces complexity which I see as being the main benefit of this rule.

I'm interested in feedback on this one!

@codecov
Copy link

codecov bot commented Sep 19, 2020

Codecov Report

Merging #3089 into master will increase coverage by 0.13%.
The diff coverage is 79.06%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3089      +/-   ##
============================================
+ Coverage     79.28%   79.41%   +0.13%     
- Complexity     2577     2584       +7     
============================================
  Files           435      436       +1     
  Lines          7766     7791      +25     
  Branches       1482     1481       -1     
============================================
+ Hits           6157     6187      +30     
  Misses          819      819              
+ Partials        790      785       -5     
Impacted Files Coverage Δ Complexity Δ
...urbosch/detekt/rules/bugs/ImplicitDefaultLocale.kt 74.19% <0.00%> (ø) 9.00 <0.00> (ø)
...lab/arturbosch/detekt/api/internal/Suppressions.kt 76.00% <50.00%> (+2.08%) 0.00 <0.00> (ø)
...gitlab/arturbosch/detekt/api/AnnotationExcluder.kt 71.42% <75.00%> (+35.06%) 7.00 <0.00> (ø)
...rturbosch/detekt/rules/style/UnusedPrivateClass.kt 71.42% <83.33%> (+5.25%) 4.00 <0.00> (ø)
...kt/rules/complexity/ReplaceSafeCallChainWithRun.kt 86.66% <86.66%> (ø) 7.00 <7.00> (?)
...osch/detekt/rules/complexity/ComplexityProvider.kt 100.00% <100.00%> (ø) 3.00 <0.00> (ø)
...rbosch/detekt/rules/complexity/TooManyFunctions.kt 98.43% <100.00%> (+1.66%) 25.00 <4.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 e52f6f2...0325a71. 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.

Wow, neat trick. Those endless safe chains are always a problem for me. They are hard to understand what can really be null. Even for one is worth it. 👏🏻👏🏻 Nice one.

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.

I agree. This rule is a nice addition. Great idea! 👍 🥇

Copy link
Member

@cortinico cortinico left a comment

Choose a reason for hiding this comment

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

Great addition 👍

@arturbosch arturbosch added this to the 1.14.0 milestone Sep 26, 2020
@arturbosch arturbosch merged commit ef3914b into detekt:master Sep 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants