Skip to content

Mutating through Option::inspect gives bogus suggestion #128381

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

Open
Swatinem opened this issue Jul 30, 2024 · 0 comments
Open

Mutating through Option::inspect gives bogus suggestion #128381

Swatinem opened this issue Jul 30, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Swatinem
Copy link
Contributor

Code

#[derive(Debug)]
struct A {
    a: u32,
}

fn main() {
    let mut opt = Some(A { a: 123 });
    let ref_mut_opt = opt.as_mut().inspect(|a| {
        a.a += 123;
    });
    dbg!(ref_mut_opt);
}

Current output

error[E0594]: cannot assign to `a.a`, which is behind a `&` reference
  --> src/main.rs:18:9
   |
17 |     let ref_mut_opt = opt.as_mut().inspect(|a| {
   |                                             - consider changing this binding's type to be: `&mut &mut A`
18 |         a.a += 123;
   |         ^^^^^^^^^^ `a` is a `&` reference, so the data it refers to cannot be written

Desired output

No response

Rationale and extra context

The compiler suggests to "consider changing this binding's type". But Option::inspect is outside my control, and it will give me a & &mut A. There is nothing for me to change there.

Here is a playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=f54e1ae60f8e1a790c6a6379afe746fa

The code comes from a broken clippy suggestion which I also raised here: rust-lang/rust-clippy#13185

Other cases

No response

Rust Version

rustc 1.82.0-nightly (7120fdac7 2024-07-25)
binary: rustc
commit-hash: 7120fdac7a6e55a5e4b606256042890b36067052
commit-date: 2024-07-25
host: x86_64-apple-darwin
release: 1.82.0-nightly
LLVM version: 18.1.7

Anything else?

No response

@Swatinem Swatinem added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 30, 2024
@jieyouxu jieyouxu added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants