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

question_mark lint gives invalid suggestion on nightly #12412

Closed
cvijdea-bd opened this issue Mar 4, 2024 · 0 comments · Fixed by #12563
Closed

question_mark lint gives invalid suggestion on nightly #12412

cvijdea-bd opened this issue Mar 4, 2024 · 0 comments · Fixed by #12563
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@cvijdea-bd
Copy link

Summary

Applying the suggestion results in a compilation error.

error[E0507]: cannot move out of `x.a` which is behind a shared reference
   --> src\main.rs:10:13
    |
10  |    let a = &x.a?;
    |             ^^^-
    |             |
    |             `x.a` moved due to this method call
    |             move occurs because `x.a` has type `std::option::Option<std::vec::Vec<u8>>`, which does not implement the `Copy` trait

Lint Name

question_mark

Reproducer

I tried this code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=92ae818b63db5e45e9c03f8c3f79e2bd

struct Test {
    a: Option<Vec<u8>>,
}

fn test(x: &Test) -> Option<u8> {
    let Some(a) = &x.a else {
        return None;
    };
//   let a = &x.a?;
   
   a.first().copied()
}


fn main() {
   println!("{:?}", test(&Test { a: None }));
}

I saw this happen:

warning: this `let...else` may be rewritten with the `?` operator
 --> src\main.rs:7:3
  |
7 | /   let Some(a) = &x.a else {
8 | |       return None;
9 | |   };
  | |____^ help: replace it with: `let a = &x.a?;`
  |
  = 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: `test-question-mark` (bin "test-question-mark") generated 1 warning

I expected to see this happen:
no warning

Version

rustc 1.78.0-nightly (516b6162a 2024-03-03)
binary: rustc
commit-hash: 516b6162a2ea8e66678c09e8243ebd83e4b8eeea
commit-date: 2024-03-03
host: x86_64-pc-windows-msvc
release: 1.78.0-nightly
LLVM version: 18.1.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@cvijdea-bd cvijdea-bd 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 Mar 4, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Mar 4, 2024
@bors bors closed this as completed in e0e7ee1 Mar 30, 2024
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants