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

Incorrect suggestion made by question_mark when match ergonomics involved #11983

Closed
Plecra opened this issue Dec 19, 2023 · 2 comments · Fixed by #12563
Closed

Incorrect suggestion made by question_mark when match ergonomics involved #11983

Plecra opened this issue Dec 19, 2023 · 2 comments · Fixed by #12563
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@Plecra
Copy link

Plecra commented Dec 19, 2023

Summary

Possibly related to #11764

Reproducer

I tried this code:

fn foo() -> Option<()> {
let alias = Some(String::new());
let Some(v) = &alias else { return None };
let _ = v;
Some(())
}

I expected to see this happen:

A valid suggestion would be produced. This is non-trivial, since clippy wants to remove the match. as_ref() works for Options, but not in the general case

Instead, this happened:

warning: this `let...else` may be rewritten with the `?` operator
 --> src/main.rs:4:1
  |
4 | let Some(v) = &alias else { return None };
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `let v = &alias?;`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
  = note: `#[warn(clippy::question_mark)]` on by default

warning: `playground` (bin "playground") generated 1 warning

note: The suggestion doesn't compile as it has different semantics, and applies the Try operator before taking the reference.

Version

On the playground, which indicates version 0.1.74 (2023-12-04 a28077b)

Additional Labels

No response

@Plecra Plecra added the C-bug Category: Clippy is not doing the correct thing label Dec 19, 2023
@cvijdea-bd
Copy link

Might be related to #12412 (or even a duplicate)

@J-ZhengLi
Copy link
Member

J-ZhengLi commented Mar 21, 2024

Yet another reproducer that actually fail to compile (might be different from what @Plecra described, as reason of this failure was because &Option doesn't impl Try):

fn foo(alias: &Option<String>) -> Option<()> {
    let Some(v) = &alias else { return None };
    // let v = &alias?;
    Some(())
}

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants