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

needless_return_with_question_mark when inside let ... else #11613

Closed
chris13524 opened this issue Oct 5, 2023 · 1 comment
Closed

needless_return_with_question_mark when inside let ... else #11613

chris13524 opened this issue Oct 5, 2023 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@chris13524
Copy link

Summary

I use a return so that my type checks pass, but this lint now wants me to remove the return which breaks my types.

Lint Name

needless_return_with_question_mark

Reproducer

I tried this code:

fn try_fn() -> anyhow::Result<()> {
    let val = Some("hi");
    let Some(val) = val else {
        return Err(anyhow::anyhow!("the error"))?;
    };
    println!("val: {val}");
    Ok(())
}

I saw this happen:

warning: unneeded `return` statement with `?` operator
 --> src/main.rs:8:9
  |
8 |         return Err(anyhow::anyhow!("the error"))?;
  |         ^^^^^^^ help: remove it
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return_with_question_mark
  = note: `#[warn(clippy::needless_return_with_question_mark)]` on by default

warning: `needless_return_with_question_mark` (bin "needless_return_with_question_mark") generated 1 warning (run `cargo clippy --fix --bin "needless_return_with_question_mark"` to apply 1 suggestion)

But when I fix that:

fn try_fn() -> anyhow::Result<()> {
    let val = Some("hi");
    let Some(val) = val else {
        Err(anyhow::anyhow!("the error"))?;
    };
    println!("val: {val}");
    Ok(())
}

I now get this error:

error[E0308]: `else` clause of `let...else` does not diverge
 --> src/main.rs:7:30
  |
7 |       let Some(val) = val else {
  |  ______________________________^
8 | |         Err(anyhow::anyhow!("the error"))?;
9 | |     };
  | |_____^ expected `!`, found `()`
  |
  = note:   expected type `!`
          found unit type `()`
  = help: try adding a diverging expression, such as `return` or `panic!(..)`
  = help: ...or use `match` instead of `let...else`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `needless_return_with_question_mark` (bin "needless_return_with_question_mark") due to previous error

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: aarch64-apple-darwin
release: 1.73.0
LLVM version: 17.0.2

Additional Labels

No response

@chris13524 chris13524 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Oct 5, 2023
@dswij
Copy link
Member

dswij commented Nov 28, 2023

Should be fixed by #11802

@dswij dswij closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants