-
-
Notifications
You must be signed in to change notification settings - Fork 823
Create Rule: MapGetWithNotNullAssertionOperator #2171
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
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
detekt-rules/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/MapGetWithNotNullAssert.kt
Show resolved
Hide resolved
...-rules/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/MapGetWithNotNullAssertSpec.kt
Outdated
Show resolved
Hide resolved
…tWithNotNullAssert
schalkms
left a comment
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.
Thanks for contributing this rule!
This is an awesome first contribution! 🎉
I attached my feedback and one question to this code review.
detekt-rules/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/MapGetWithNotNullAssert.kt
Outdated
Show resolved
Hide resolved
detekt-rules/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/MapGetWithNotNullAssert.kt
Outdated
Show resolved
Hide resolved
| ) | ||
|
|
||
| override fun visitPostfixExpression(expression: KtPostfixExpression) { | ||
| if (expression.isMapGet() && expression.operationToken == KtTokens.EXCLEXCL) { |
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.
What about this check from the original rule?
I think it's missing
if (expression.getReplacementData() == null) returnThere 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.
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.
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.
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.
...-rules/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/MapGetWithNotNullAssertSpec.kt
Outdated
Show resolved
Hide resolved
...-rules/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/MapGetWithNotNullAssertSpec.kt
Outdated
Show resolved
Hide resolved
3flex
left a comment
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.
This rule is great! Well done. Once you've addressed feedback from @schalkms and myself I'd be happy to merge this.
detekt-rules/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/MapGetWithNotNullAssert.kt
Outdated
Show resolved
Hide resolved
…/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>
…to rule-mapgetwithnotnullassert
This PR adds new rule
MapGetWithNotNullAssertionOperatortopotential-bugsruleset.Rule
MapGetWithNotNullAssertionOperatorreports not-null asserted calls ofmap.get()!!ormap[]!!methods and recommends to usemap.getValue(),map.getOrDefault()ormap.getOrElse().This rule is based on an inspection MapGetWithNotNullAssertionOperatorInspection from IntelliJ IDEA.
Noncompliant Code
Compliant Code