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

Create Rule: MapGetWithNotNullAssertionOperator #2171

Merged
merged 6 commits into from Dec 10, 2019

Conversation

smyachenkov
Copy link
Contributor

@smyachenkov smyachenkov commented Dec 9, 2019

This PR adds new rule MapGetWithNotNullAssertionOperator to potential-bugs ruleset.

Rule MapGetWithNotNullAssertionOperator reports not-null asserted calls of map.get()!! or map[]!! methods and recommends to use map.getValue(), map.getOrDefault() or map.getOrElse().

This rule is based on an inspection MapGetWithNotNullAssertionOperatorInspection from IntelliJ IDEA.

Noncompliant Code

val map = emptyMap<String, String>()
map["key"]!!
val map = emptyMap<String, String>()
map.get("key")!!

Compliant Code

val map = emptyMap<String, String>()
map["key"]
val map = emptyMap<String, String>()
map.getValue("key")
val map = emptyMap<String, String>()
map.getOrDefault("key", "")
val map = emptyMap<String, String>()
map.getOrElse("key", { "" })

@codecov-io
Copy link

codecov-io commented Dec 9, 2019

Codecov Report

Merging #2171 into master will increase coverage by 0.01%.
The diff coverage is 85.71%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2171      +/-   ##
============================================
+ Coverage     80.58%   80.59%   +0.01%     
- Complexity     2024     2028       +4     
============================================
  Files           337      338       +1     
  Lines          5836     5850      +14     
  Branches       1065     1066       +1     
============================================
+ Hits           4703     4715      +12     
  Misses          564      564              
- Partials        569      571       +2
Impacted Files Coverage Δ Complexity Δ
...sch/detekt/rules/providers/PotentialBugProvider.kt 100% <100%> (ø) 3 <0> (ø) ⬇️
...t/rules/bugs/MapGetWithNotNullAssertionOperator.kt 84.61% <84.61%> (ø) 4 <4> (?)

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 ffbd6e1...86b8dae. Read the comment docs.

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.

Thanks for contributing this rule!
This is an awesome first contribution! 🎉
I attached my feedback and one question to this code review.

)

override fun visitPostfixExpression(expression: KtPostfixExpression) {
if (expression.isMapGet() && expression.operationToken == KtTokens.EXCLEXCL) {
Copy link
Member

Choose a reason for hiding this comment

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

What about this check from the original rule?
I think it's missing

if (expression.getReplacementData() == null) return

Copy link
Member

Choose a reason for hiding this comment

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

Looking at the IDEA implementation I think that's only used to make sure it's possible for IDEA to recommend a quick fix, I don't think it's used to check for the issue itself. The tests in this PR look comprehensive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is correct, getReplacementData function in original inspection is used for generation of the replacement code. I left it out because I think it's enough to check that expression.operationToken is !! and that is expression on the left is resolved to Map.get.

Copy link
Member

@3flex 3flex left a comment

Choose a reason for hiding this comment

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

This rule is great! Well done. Once you've addressed feedback from @schalkms and myself I'd be happy to merge this.

smyachenkov and others added 4 commits December 10, 2019 10:15
…/bugs/MapGetWithNotNullAssert.kt

Co-Authored-By: M Schalk <30376729+schalkms@users.noreply.github.com>
…/bugs/MapGetWithNotNullAssert.kt

Co-Authored-By: M Schalk <30376729+schalkms@users.noreply.github.com>
@smyachenkov smyachenkov changed the title Create Rule: MapGetWithNotNullAssert Create Rule: MapGetWithNotNullAssertionOperator Dec 10, 2019
@schalkms schalkms merged commit d08e13c into detekt:master Dec 10, 2019
@arturbosch arturbosch added this to the 1.3.0 milestone Dec 22, 2019
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.

None yet

6 participants