FP: needless_return_with_question_mark with implicit Error Conversion

Return with a question mark was triggered in situations where the `?`
desuraging was performing error conversion via `Into`/`From`.

The desugared `?` produces a match over an expression with type
`std::ops::ControlFlow<B,C>` with `B:Result<Infallible, E:Error>` and
`C:Result<_, E':Error>`, and the arms perform the conversion. The patch
adds another check in the lint that checks that `E == E'`. If `E == E'`,
then the `?` is indeed unnecessary.

changelog: False Positive: `needless_return_with_question_mark` when
implicit Error Conversion occurs.
This commit is contained in:
Quinn Sinclair 2023-12-26 20:34:00 +02:00 committed by PartiallyTyped
parent 9dd2252b2c
commit 57dd25e2ff
3 changed files with 81 additions and 1 deletions

View file

@ -77,3 +77,30 @@ fn issue11616() -> Result<(), ()> {
};
Ok(())
}
/// This is a false positive that occurs because of the way `?` is handled.
/// The `?` operator is also doing a conversion from `Result<T, E>` to `Result<T, E'>`.
/// In this case the conversion is needed, and thus the `?` operator is also needed.
fn issue11982() {
mod bar {
pub struct Error;
pub fn foo(_: bool) -> Result<(), Error> {
Ok(())
}
}
pub struct Error;
impl From<bar::Error> for Error {
fn from(_: bar::Error) -> Self {
Error
}
}
fn foo(ok: bool) -> Result<(), Error> {
if !ok {
return bar::foo(ok).map(|_| Ok::<(), Error>(()))?;
};
Ok(())
}
}

View file

@ -77,3 +77,30 @@ fn issue11616() -> Result<(), ()> {
};
Ok(())
}
/// This is a false positive that occurs because of the way `?` is handled.
/// The `?` operator is also doing a conversion from `Result<T, E>` to `Result<T, E'>`.
/// In this case the conversion is needed, and thus the `?` operator is also needed.
fn issue11982() {
mod bar {
pub struct Error;
pub fn foo(_: bool) -> Result<(), Error> {
Ok(())
}
}
pub struct Error;
impl From<bar::Error> for Error {
fn from(_: bar::Error) -> Self {
Error
}
}
fn foo(ok: bool) -> Result<(), Error> {
if !ok {
return bar::foo(ok).map(|_| Ok::<(), Error>(()))?;
};
Ok(())
}
}