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

Implement new lint: if_then_some_else_none #6859

Merged
merged 4 commits into from
Mar 13, 2021

Conversation

magurotuna
Copy link
Contributor

Resolves #6760

changelog: Added a new lint: if_then_some_else_none

@rust-highfive
Copy link

r? @Manishearth

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 6, 2021
Copy link
Contributor

@giraffate giraffate left a comment

Choose a reason for hiding this comment

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

@magurotuna
Copy link
Contributor Author

@giraffate thanks for pointing that out, that's absolutely correct! Fixed it now.

@Manishearth
Copy link
Member

r? @giraffate do you want to take a shot at reviewing this?

@giraffate
Copy link
Contributor

Yes, I'll do!

@Manishearth
Copy link
Member

@bors delegate=giraffate

@bors
Copy link
Contributor

bors commented Mar 9, 2021

✌️ @giraffate can now approve this pull request

Comment on lines 85 to 86
let cond_snip = utils::snippet(cx, cond.span, "[condition]");
let arg_snip = utils::snippet(cx, then_arg.span, "");
Copy link
Contributor

Choose a reason for hiding this comment

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

Can test codes like below be added? Using macro, the suggested code may be broken, so it would be better to use the method like snippet_with_macro_callsite.

    let _ = if matches!(true, true) {
        println!("true!");
        Some(matches!(true, false))
    } else {
        None
    };

Additionally, can test codes like below be added? The parenthesis isn't considered, so the suggested codes seem not to work.

    let x = Some(5);
    let _ = x.and_then(|o| if o < 32 { Some(o) } else { None });

let cond_snip = utils::snippet(cx, cond.span, "[condition]");
let arg_snip = utils::snippet(cx, then_arg.span, "");
let help = format!(
"consider using `bool::then` like: `{}.then(|| {{ /* snippet */ {} }})`",
Copy link
Contributor

Choose a reason for hiding this comment

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

If then block has no statements, it would be better to remove the placeholder like /* snippet */.

@giraffate giraffate added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 12, 2021
@magurotuna
Copy link
Contributor Author

@giraffate
Thanks a lot for the review!
I addressed your points:

  • use snippet_with_macro_callsite instead
  • make a condition of if expression parenthesized when it is a unary or binary expression
  • if a then block has no statements, don't display /* snippet */ and curly braces of a closure.

Please take a look again :)

@giraffate
Copy link
Contributor

@bors r+

It looks good. Thanks!

@bors
Copy link
Contributor

bors commented Mar 13, 2021

📌 Commit 11d2af7 has been approved by giraffate

@bors
Copy link
Contributor

bors commented Mar 13, 2021

⌛ Testing commit 11d2af7 with merge 781de34...

@bors
Copy link
Contributor

bors commented Mar 13, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: giraffate
Pushing 781de34 to master...

@bors bors merged commit 781de34 into rust-lang:master Mar 13, 2021
@magurotuna magurotuna deleted the if_then_some_else_none branch March 13, 2021 16:38
/// 42
/// });
/// ```
pub IF_THEN_SOME_ELSE_NONE,
Copy link
Member

Choose a reason for hiding this comment

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

Since the MSRV feature is pretty new an we don't have any automated checks for it yet:

If an MSRV is added to a lint, the lint has also to be added in the config option. I wrote documentation about this in #6977. cc @rust-lang/clippy

Copy link
Member

Choose a reason for hiding this comment

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

I will prepare a fix for this (and possible other lints soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If an MSRV is added to a lint, the lint has also to be added in the config option.

Oh I didn't know that. I'll keep that in mind for the next time I implement a new lint.

I will prepare a fix for this (and possible other lints soon.

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for your follow-up!

Copy link
Member

Choose a reason for hiding this comment

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

Oh I didn't know that. I'll keep that in mind for the next time I implement a new lint.

No worries, this isn't/wasn't documented anywhere, so that's on me for missing it while reviewing the documentation.

bors added a commit that referenced this pull request May 6, 2021
…, r=flip1995

Add a missing lint to MSRV config doc

A follow-up of #6859.

changelog: Add a missing lint to MSRV config doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: prefer bool::then
6 participants