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

Suggest changing if let Some(3) = x to if x == Some(3) #7554

Closed
adamchalmers opened this issue Aug 11, 2021 · 2 comments
Closed

Suggest changing if let Some(3) = x to if x == Some(3) #7554

adamchalmers opened this issue Aug 11, 2021 · 2 comments
Labels
A-lint Area: New lints

Comments

@adamchalmers
Copy link

What it does

I was mentoring a new Rust programmer on my team, and he was a bit confused by if let. The root of his confusion was that he thought if let is used to introduce a binding. I agreed with him! But then he showed me a case where you can use if let without binding:

if let Some(3) = x {
   println!("it's true);
}

I told him, "yeah, if I saw that in code review, I would suggest using if x == Some(3) instead.

Categories (optional)

  • Kind: style

What is the advantage of the recommended code over the original code

I think it's simpler, it uses a more standard language construct. You're just evaluating a boolean condition. IMO you should only use if let if you're actually trying to pattern match, but simply checking equality doesn't require pattern matching!

Drawbacks

This is basically just a generalization of redundant_pattern_matching, so it might have the same problems with ordering which that lint has.

Example

if let Some(3) = x {
   println!("it's true);
}

Could be written as:

if x == Some(3) {
   println!("it's true);
}
@adamchalmers adamchalmers added the A-lint Area: New lints label Aug 11, 2021
@camsteffen
Copy link
Contributor

I guess I'd call this needless_if_let. It's not terrible style, but I agree that == is better, more idiomatic. Another aspect of the issue is that it's a yoda condition. I think this qualifies for style, but I could also understand pedantic.

@camsteffen
Copy link
Contributor

Duplicate of #1716

@camsteffen camsteffen marked this as a duplicate of #1716 Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants