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 UseLet style rule #6091

Merged
merged 6 commits into from May 17, 2023
Merged

Add UseLet style rule #6091

merged 6 commits into from May 17, 2023

Conversation

tresni
Copy link
Contributor

@tresni tresni commented May 11, 2023

I asked a question about my rule in the #detekt channel, and there seemed to be some appetite for the rule. This is what my rule currently looks like.

I found that in a code base I was working with, there were a lot of instances of if (x != null) { do_something(x) } else null and wanted to encourage the use of ?.let in this specific case. The plugin also looks for if (x == null) { null } else do_something(x) as we had some of that too.

@detekt-ci
Copy link
Collaborator

detekt-ci commented May 11, 2023

Messages
📖 Thanks for adding a new rule to detekt ❤️
We detekted that you added tests, to your rule, that's awesome!

We detekted that you updated the default-detekt-config.yml file, that's awesome!

Generated by 🚫 dangerJS against 49a5a00

@codecov
Copy link

codecov bot commented May 12, 2023

Codecov Report

Merging #6091 (49a5a00) into main (a8936c1) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@             Coverage Diff              @@
##               main    #6091      +/-   ##
============================================
+ Coverage     84.83%   84.86%   +0.02%     
- Complexity     3997     4011      +14     
============================================
  Files           567      568       +1     
  Lines         13400    13422      +22     
  Branches       2362     2368       +6     
============================================
+ Hits          11368    11390      +22     
  Misses          871      871              
  Partials       1161     1161              
Impacted Files Coverage Δ
...rturbosch/detekt/rules/style/StyleGuideProvider.kt 100.00% <100.00%> (ø)
.../io/gitlab/arturbosch/detekt/rules/style/UseLet.kt 100.00% <100.00%> (ø)

Copy link
Member

@BraisGabin BraisGabin left a comment

Choose a reason for hiding this comment

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

LGTM. Just remove the documentation update so we can talk about it in other pr because I'm not 100% sure about it. But that shouldn't block the rest of the pr.

@marschwar
Copy link
Contributor

Thank you for the rule. It looks really good.

I know this style of tests is really popular but to me it feels harder to read than necessary and hard to maintain. But I may be the only one to see it that way.

I agree with @BraisGabin that the documentation change should be its own pr.

@BraisGabin
Copy link
Member

I agree they are hard to read. But test all the cases possible cases is really nice. All those tests would be a nightmare to maintain too if they are duplicated time over time... It's a difficult compromise.

@marschwar
Copy link
Contributor

I think I might have found a false positive.

    fun testCallToCreateTempFile(s: String?) {
        val x = if (s == null) {
            println("foo")
            null
        } else {
            "x"
        }
    }

In my opinion this cannot be easily rewritten using s?.let.

@tresni
Copy link
Contributor Author

tresni commented May 13, 2023

@marschwar I agree that that is the exception case. We are using that as the times to use @Suppress as I don't know a meaningful way to exclude that. In our code base, we found only a single time that we were doing that vs ~100 cases where we should have just used let. That hit rate was acceptable for us.

It was pointed out that you could rewrite that as s?.let { "X" } ?: run { println("foo") } but internally we decided that wasn't really better.

@atulgpt
Copy link
Contributor

atulgpt commented May 14, 2023

@marschwar I agree that that is the exception case. We are using that as the times to use @Suppress as I don't know a meaningful way to exclude that. In our code base, we found only a single time that we were doing that vs ~100 cases where we should have just used let. That hit rate was acceptable for us.

It was pointed out that you could rewrite that as s?.let { "X" } ?: run { println("foo") } but internally we decided that wasn't really better.

Hi, @tresni thanks for contributing towards the detekt rule. Also regarding the above false positive you can ignore that if else branch has more statements other than returning null

@BraisGabin
Copy link
Member

Also regarding the above false positive you can ignore that if else branch has more statements other than returning null

Yes. And I think that with that, and a test to demonstrate it, this rule is ready to merge.

Copy link
Contributor

@marschwar marschwar left a comment

Choose a reason for hiding this comment

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

Great work.

@detekt/maintainers We could enable it for the detekt codebase right away. There are no findings.

@marschwar
Copy link
Contributor

@tresni Could you please activate your rule in the config/detekt/detekt.yml.

@marschwar marschwar added this to the 1.23.0 milestone May 16, 2023
@marschwar marschwar merged commit 4e8fe6b into detekt:main May 17, 2023
23 checks passed
@cortinico cortinico added the notable changes Marker for notable changes in the changelog label May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
notable changes Marker for notable changes in the changelog rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants