Skip to content

Refactor + rename util function inside MandatoryBracesIfStatement rule #3908

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 3 commits into from
Jun 28, 2021
Merged

Refactor + rename util function inside MandatoryBracesIfStatement rule #3908

merged 3 commits into from
Jun 28, 2021

Conversation

schalkms
Copy link
Member

No description provided.

this.`else` !is KtIfExpression &&
this.`else` !is KtBlockExpression &&
this.`else` !is KtWhenExpression
private fun hasCorrectElseType(expression: KtExpression): Boolean =
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe you have better ideas for the function name.

Copy link
Contributor

@marschwar marschwar Jun 26, 2021

Choose a reason for hiding this comment

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

hasCorrectElseType is really wrong. It should be hasIncorrectElseType.

I played around with it a little bit and realized that we do allow a when expression within the else branch but not in the then branch. It may not be something that is used often, but I don't see a reason why this is compliant

fun f(i: Int) {
    if (true) {
        println()
    } else when(i) {
        1 -> println(1)
        else -> println()
    }
}

while this is not

fun f(i: Int) {
    if (true) when(i) {
        1 -> println(1)
        else -> println()
    } else when(i) {
        println()
    }
}

I ended up with

val thenExpression = expression.then
if (mustBeOnSameLine(thenExpression) && hasNewLineAfter(expression.rightParenthesis)) {
    report(CodeSmell(issue, Entity.from(thenExpression ?: expression), DESCRIPTION))
}

val elseExpression = expression.`else`
if (mustBeOnSameLine(elseExpression) && hasNewLineAfter(expression.elseKeyword)) {
    report(CodeSmell(issue, Entity.from(elseExpression ?: expression), DESCRIPTION))
}

private fun mustBeOnSameLine(expression: KtExpression?): Boolean {
    return expression != null &&
        expression !is KtIfExpression &&
        expression !is KtBlockExpression &&
        expression !is KtWhenExpression
}

Copy link
Member Author

@schalkms schalkms Jun 26, 2021

Choose a reason for hiding this comment

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

Thanks for the suggestion. I have the feeling that experimental contracts are a bit overkill in this case. I'm also not sure whether we want experimental features being used in detekt's code base.

Copy link
Contributor

Choose a reason for hiding this comment

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

I totally agree. I updated the comment above removing the contract.

@codecov
Copy link

codecov bot commented Jun 26, 2021

Codecov Report

Merging #3908 (83f90ad) into main (44c1ef0) will increase coverage by 0.03%.
The diff coverage is 84.61%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #3908      +/-   ##
============================================
+ Coverage     83.53%   83.56%   +0.03%     
+ Complexity     3120     3118       -2     
============================================
  Files           456      456              
  Lines          8996     8995       -1     
  Branches       1753     1750       -3     
============================================
+ Hits           7515     7517       +2     
  Misses          565      565              
+ Partials        916      913       -3     
Impacted Files Coverage Δ
...ules/style/optional/MandatoryBracesIfStatements.kt 86.66% <84.61%> (+17.91%) ⬆️

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 44c1ef0...83f90ad. Read the comment docs.

report(CodeSmell(issue, Entity.from(expression.`else` ?: expression), DESCRIPTION))
val elseExpression = expression.`else`
if (elseExpression != null && hasCorrectElseType(elseExpression) && hasNewLine(expression.elseKeyword)) {
report(CodeSmell(issue, Entity.from(elseExpression), DESCRIPTION))
Copy link
Member Author

Choose a reason for hiding this comment

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

By doing the null check before, we can safely pass the else variable here.

@cortinico cortinico changed the title Refactor + rename MandatoryBracesIfStatement rule Refactor + rename util function inside MandatoryBracesIfStatement rule Jun 27, 2021
@cortinico cortinico added this to the 1.18.0 milestone Jun 27, 2021
@BraisGabin BraisGabin merged commit 063866c into detekt:main Jun 28, 2021
@cortinico cortinico added the housekeeping Marker for housekeeping tasks and refactorings label Jun 30, 2021
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 rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants