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

New rule: Reports the usages of <expr1>?.let { <expr2> } ?: run { <single_line_expr> } #6112

Open
atulgpt opened this issue May 19, 2023 · 11 comments

Comments

@atulgpt
Copy link
Contributor

atulgpt commented May 19, 2023

Expected Behavior of the rule

Rule should report <expr1>?.let { <expr2> } ?: run { <single_line_expr> } as expression can be simplified as <expr1>?.let { <expr2> } ?: <single_line_expr>. Here there is an unnecessary use of run { } block

uncompliant code

val nullable: Int? = 4
val default = 1
val a = nullable?.let { 0 } ?: run { default }

compliant code

val nullable: Int? = 4
val default = 1
val a = nullable?.let { 0 } ?: default

Context

Got this review in #5981 (comment) having unnecessary run { } really makes the code more indented and increases the complexity

@atulgpt atulgpt added the rules label May 19, 2023
@3flex
Copy link
Member

3flex commented May 19, 2023

Could the rule be more generic?

It looks like what we want flagged is any use of the non-extension version of run that contains a single line expression.

@atulgpt
Copy link
Contributor Author

atulgpt commented May 20, 2023

Yes, I think It can be generic. But then we also have to consider single expression use of with, apply, let and if those make sense as well or not(and then we can maybe incorporate more scoping operators). Let me update the proposal

@atulgpt
Copy link
Contributor Author

atulgpt commented May 25, 2023

Hi, @3flex got your query yes it should be generic.

We can create similar rules for with as well as apply as well
Some more violating code

val length = with("") {
       length
}
val length = "".apply {
       length
}
val a = run { "".length }

There is one similar rule UnnecessaryLet which does the same with let.

Update 29/05/23: We also have UnnecessaryApply rule

@BraisGabin
Copy link
Member

So the proposal would be to create UnnecessaryRun and UnnecessaryWith too? I would vote in favor of that. And these will not be related at all with the previous <expr1>?.let { <expr2> } ?:, right?

If that's the idea, we need proper definitions of what these rules should catch. Some more examples of compilant/non-compilant.

@atulgpt
Copy link
Contributor Author

atulgpt commented Aug 17, 2023

Yes @BraisGabin this suggestion is independent of the previous suggestion as that one deals through the case when due to the last nullable expression code execution pass through the next run or apply block as well.
This one mostly focuses on unnecessary block operators when there is only one expression

@detekt-ci
Copy link
Collaborator

This issue is stale because it has been open 90 days with no activity. Please comment or this will be closed in 7 days.

@detekt-ci
Copy link
Collaborator

This issue was closed because it has been stalled for 7 days with no activity.

@detekt-ci detekt-ci closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2023
@atulgpt
Copy link
Contributor Author

atulgpt commented Nov 24, 2023

Not stale

@cortinico cortinico reopened this Nov 24, 2023
@cortinico cortinico removed the stale label Nov 24, 2023
@BraisGabin
Copy link
Member

Sorry for the really late response. How can we make this issue actionable? Would a rule called UnnecessaryRun that detects single expresions inside run close this issue?

@atulgpt
Copy link
Contributor Author

atulgpt commented Nov 26, 2023

Yes, @BraisGabin that will close the issue. After implementing that and analyzing the learnings from that rule we can implement more rules mentioned in #6112 (comment)

@BraisGabin
Copy link
Member

Ok, so to summarize to anyone reading and willing to contribute. We want a rule called UnnecessaryRun that detects single expressions. And we are only talking about run without any receiver. T.run is another topic.

This rule can be expanded in the future but for now just that.

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

No branches or pull requests

5 participants