Rust-clippy: Lint `unwrap` in branch that did `is_some` before

Created on 5 Feb 2018  路  6Comments  路  Source: rust-lang/rust-clippy

if foo.is_some() {
    use(foo.unwrap());
}

should be

if let Some(foo) = foo {
    use(foo);
}
A-guidelines A-style A-unnecessary E-medium L-lint T-middle

Most helpful comment

Yeah I also can't think of a case that would break this with NLL. Let's move it to complexity.

All 6 comments

It needs to check for the existence of && or ||. I have written things like

if something && foo.is_some() {
    use(foo.unwrap());
}

The other alternative in my case is to have first the if let then another if something nested within. Which for some cases is over kill.

This seems to be a duplicate of #1770?

I was also surprised that Clippy doesn't catch it already. It looks like it's implemented and in "nursery":

https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap

Are there known problems preventing this from being on by default?

Known problems says "Limitations of the borrow checker might make unwrap() necessary sometimes?".

The only thing I could find, why this is in nursery is: https://github.com/rust-lang/rust-clippy/pull/2811#discussion_r193715852. Since the tests look good I would suggest to move this to complexity and see if someone complains?

NLL shipped since that comment was written, so maybe it's fine? I can't think of any borrow that would break it. .unwrap() moves, so there can't be any outstanding borrows when it runs.

Yeah I also can't think of a case that would break this with NLL. Let's move it to complexity.

Was this page helpful?
0 / 5 - 0 ratings