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

Add a rule to flag places where ifBlank and ifEmpty can be used #2840

Closed
BraisGabin opened this issue Jul 3, 2020 · 7 comments · Fixed by #3186
Closed

Add a rule to flag places where ifBlank and ifEmpty can be used #2840

BraisGabin opened this issue Jul 3, 2020 · 7 comments · Fixed by #3186

Comments

@BraisGabin
Copy link
Member

Kotlin provides the functions ifBlank and ifEmpty so we should flag the points where this functions could be used. Example:

if (api.name.isBlank()) "John" else api.name
can be replaced to:
api.name.ifBlank { "John" }

The same for ifEmpty.

Context

https://twitter.com/kotlin/status/1277897234043097090

@schalkms
Copy link
Member

schalkms commented Jul 3, 2020

This would go into the style ruleset in my point of view.

@adwsingh
Copy link

If no one is working on this, would like to take a stab at it. :)

@schalkms
Copy link
Member

Thanks @MehWhatever for the help! We really appreciate your help.
It's all yours. 🙂

@adwsingh
Copy link

Do we also want to handle things like,

if (x.isBlank()) {
       return "A"
} else {
      return x
}

Though by opening that door, it would also make sense to catch these,

if (x.isBlank()) {
       return "A"
} 
return x

Which seems to be slightly tricky to catch.

On another note do we have a style rule for this,

if(something){
   return x
}
else {
   return y
}

to return if(something) x else y

@schalkms
Copy link
Member

I think it's better to start simple and improve in subsequent PRs.

@BraisGabin
Copy link
Member Author

if (x.isBlank()) {
       return "A"
} 
return x

I think that this case can be generalized and create a new rule for it. I think that the desired code here should be:

return if (x.isBlank()) {
       "A"
} else {
       x
}

I mean. a rule that detects an if that ends with a return and followed by that a return that could be changed by an else. Do you agree? But I think that we need a different issue to track this.

On another note do we have a style rule for this,

if(something){
   return x
}
else {
   return y
}

This should be handled by formatting. This is implemented by ktlint already.

@BraisGabin
Copy link
Member Author

@MehWhatever I'm unassigning you from this issue. If you are still interested in this issue you can open the PR anyway :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants