You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
ifletSome(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
ifletSome(3) = x {println!("it's true);}
Could be written as:
if x == Some(3){println!("it's true);}
The text was updated successfully, but these errors were encountered:
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.
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 thoughtif let
is used to introduce a binding. I agreed with him! But then he showed me a case where you can useif let
without binding:I told him, "yeah, if I saw that in code review, I would suggest using
if x == Some(3)
instead.Categories (optional)
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
Could be written as:
The text was updated successfully, but these errors were encountered: