Skip to content

question_mark lint gives invalid suggestion on nightly #12412

Closed
@cvijdea-bd

Description

@cvijdea-bd

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions