Skip to content

Cleanup code #2862

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

Merged
merged 5 commits into from
Jul 13, 2020
Merged

Cleanup code #2862

merged 5 commits into from
Jul 13, 2020

Conversation

amitd291
Copy link
Contributor

@amitd291 amitd291 commented Jul 12, 2020

This PR achieves the following:

  • Use safe access ?. instead of null check
  • Reuse GuardClauses.kt common code for ThrowsCount and remove duplicate code.
  • Add test case for excluding guard clauses in ThrowsCountSpek
  • Make the extension methods internal (in GuardClauses.kt) to avoid scope pollution

…se safe access instead of null check, cleanup test comments
val findings = ReturnCount(TestConfig(mapOf(ReturnCount.EXCLUDE_GUARD_CLAUSES to "true")))
.compileAndLint(code)
assertThat(findings).hasSize(1)
}

it("should get flagged for too-complicated guard clause, with EXCLUDE_GUARD_CLAUSES off") {
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain why this testcase got removed?

Copy link
Contributor Author

@amitd291 amitd291 Jul 12, 2020

Choose a reason for hiding this comment

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

@arturbosch this test case is redundant, given it is expected to be reported even with the EXCLUDE_GUARD_CLAUSES flag's value being true, it will surely be reported when the flag's value is false.

Copy link
Member

Choose a reason for hiding this comment

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

Ah now I see. Please rename the testcase to reports a too-complicated if statement for being a guard clause

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed the test case as advised above

Copy link
Member

@arturbosch arturbosch left a comment

Choose a reason for hiding this comment

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

Looks good!

@@ -30,12 +30,12 @@ fun KtExpression.isGuardClause(): Boolean {
?: return false

return ifExpr.`else` == null &&
returnExpr === ifExpr.then?.lastBlockStatementOrThis()
returnExpr == ifExpr.then?.lastBlockStatementOrThis()
Copy link
Member

Choose a reason for hiding this comment

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

=== is used here on purpose.
The return expr is the same node found as the last statement in the if block.
Or am I missing something?

Copy link
Contributor Author

@amitd291 amitd291 Jul 12, 2020

Choose a reason for hiding this comment

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

I see, so we need to match the reference rather than the structure of the node. @arturbosch if you confirm I will revert this change.
In any case, the test cases pass with the structural equality == check too.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please revert this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is reverted now.

@arturbosch arturbosch added this to the 1.10.1 milestone Jul 12, 2020
@arturbosch arturbosch added the housekeeping Marker for housekeeping tasks and refactorings label Jul 12, 2020
amitd291 added 2 commits July 13, 2020 02:09
…r ThrowsCount, add test case for the same in ThrowsCountSpec
@amitd291
Copy link
Contributor Author

@arturbosch I have done the suggested changes. I have also found and cleaned up code duplication in ThrowsCount. Please review the same and let me know of any suggestions / clarifications.

@codecov
Copy link

codecov bot commented Jul 12, 2020

Codecov Report

Merging #2862 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #2862   +/-   ##
=========================================
  Coverage     80.65%   80.66%           
+ Complexity     2346     2343    -3     
=========================================
  Files           388      388           
  Lines          7037     7035    -2     
  Branches       1288     1288           
=========================================
- Hits           5676     5675    -1     
  Misses          716      716           
+ Partials        645      644    -1     
Impacted Files Coverage Δ Complexity Δ
.../io/gitlab/arturbosch/detekt/rules/GuardClauses.kt 87.50% <100.00%> (+5.14%) 0.00 <0.00> (ø)
...itlab/arturbosch/detekt/rules/style/ThrowsCount.kt 100.00% <100.00%> (ø) 7.00 <0.00> (-3.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 1ec8d1f...2f4b11b. Read the comment docs.

val returnExpr = this.findDescendantOfType<KtReturnExpression>() ?: return false
return isIfConditionGuardClause(returnExpr) || returnExpr.isElvisOperatorGuardClause()
}

fun KtExpression.isElvisOperatorGuardClause(): Boolean {
Copy link
Member

Choose a reason for hiding this comment

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

Could this be internal? I'm a bit worried about the scope polution... I'm going to open an issue to see if we can create some rule for this cases.

Copy link
Contributor Author

@amitd291 amitd291 Jul 12, 2020

Choose a reason for hiding this comment

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

@BraisGabin sounds like a good idea. I would recommend it to be tackled in a separate issue as you suggested.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, I just created #2863. But, any way, can we make this function internal?

Copy link
Contributor Author

@amitd291 amitd291 Jul 13, 2020

Choose a reason for hiding this comment

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

@BraisGabin I have made this extension function internal (made the other one in the file internal as well)
cc @arturbosch

Copy link
Member

@arturbosch arturbosch left a comment

Choose a reason for hiding this comment

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

Cool, thanks!

@arturbosch arturbosch merged commit bf6f979 into detekt:master Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
housekeeping Marker for housekeeping tasks and refactorings
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants