Summary
Seems like it might be related to the fix for #16958
In this case, it appears that the borrow checker can reason that return Err(...)?; will exit, but cannot tell that Err(...)? alone will, presumably it cannot tell that no Ok branch which could fall through will reach the question_mark.
Lint Name
clippy::needless_return_with_question_mark
Reproducer
I tried this code:
use std::error::Error;
#[derive(Debug)]
struct Foo<'a> {
x: &'a str
}
impl<'a> Foo<'a> {
fn hmm(x: &'a str, y: &'a str) -> Result<(Foo<'a>, Foo<'a>), String> {
Ok((Foo { x }, Foo{ x: y }))
}
}
fn main() -> anyhow::Result<(), Box<dyn Error>> {
let v: Result<(Foo<'_>, Foo<'_>), String> = Foo::hmm("uhh", "umm");
if let Err(_u) = v {
return Err("bar".to_string())?;
}
let (x, _) = v.unwrap();
eprintln!("{:?}", x.x);
Ok(())
}
I saw this happen:
Checking playground v0.0.1 (/playground)
warning: unneeded `return` statement with `?` operator
--> src/main.rs:17:9
|
17 | return Err("bar".to_string())?;
| ^^^^^^^ help: remove it
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.97.0/index.html#needless_return_with_question_mark
= note: `#[warn(clippy::needless_return_with_question_mark)]` on by default
Following the suggestion leads to this:
error[E0382]: use of partially moved value: `v`
--> src/main.rs:19:18
|
16 | if let Err(_u) = v {
| -- value partially moved here
...
19 | let (x, _) = v.unwrap();
| ^ value used here after partial move
|
= note: partial move occurs because value has type `String`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
16 | if let Err(ref _u) = v {
| +++
We couldn't easily borrow the _u value, because we required a trait not implemented for references.
Version
Additional Labels
@rustbot label +I-suggestion-causes-error
Summary
Seems like it might be related to the fix for #16958
In this case, it appears that the borrow checker can reason that
return Err(...)?;will exit, but cannot tell thatErr(...)?alone will, presumably it cannot tell that no Ok branch which could fall through will reach the question_mark.Lint Name
clippy::needless_return_with_question_mark
Reproducer
I tried this code:
I saw this happen:
Following the suggestion leads to this:
We couldn't easily borrow the
_uvalue, because we required a trait not implemented for references.Version
Additional Labels
@rustbot label +I-suggestion-causes-error