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

manual_inspect suggestion breaks mutating code #13185

Open
Swatinem opened this issue Jul 30, 2024 · 6 comments · May be fixed by #13609
Open

manual_inspect suggestion breaks mutating code #13185

Swatinem opened this issue Jul 30, 2024 · 6 comments · May be fixed by #13609
Assignees
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

@Swatinem
Copy link
Contributor

Swatinem commented Jul 30, 2024

Summary

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

The suggestion leads to broken code which does not compile:

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

Using inspect, I end up with & &mut A, and I can’t seem to be able to mutate things through that reference.
Changing the signature as suggested in the second compiler error is also not possible.

Lint Name

manual_inspect

Reproducer

I tried this code:

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

fn main() {
    // warns but works:
    let mut opt = Some(A { a: 123 });
    let ref_mut_opt = opt.as_mut().map(|a| {
        a.a += 123;
        a
    });
    dbg!(ref_mut_opt);
}

I saw this happen:

warning: using `map` over `inspect`
 --> src/main.rs:9:36
  |
9 |     let ref_mut_opt = opt.as_mut().map(|a| {
  |                                    ^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_inspect
  = note: `#[warn(clippy::manual_inspect)]` on by default
help: try
  |
9 ~     let ref_mut_opt = opt.as_mut().inspect(|a| {
10~         a.a += 123;
  |

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

Additional Labels

No response

@Swatinem Swatinem 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 Jul 30, 2024
@Alexendoo Alexendoo added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 30, 2024
@kyoto7250
Copy link
Contributor

@rustbot claim

@asibahi
Copy link

asibahi commented Sep 5, 2024

Hit this exact issue (a d the other linked issue) in this code

@leonardoarcari
Copy link

Same. Hit the same issue with this code. Had to suppress the warning for now but I don't think it's a case of manual inspect

@Samuel-B-D
Copy link

This is still an issue, and this clippy is now stabilized and enabled by default on the latest stable release.
I'm silencing it for now but bumping this issue since it's a false-positive with a compilation-breaking suggestion that is now enabled by default.

@kyoto7250
Copy link
Contributor

I still have the will to work on this issue, but I've been busy with work for a while, so I don't think I'll be able to do it this month.

If there is someone who is interested, it would be nice if that person could be in charge.

@SpriteOvO
Copy link
Contributor

@rustbot claim

@rustbot rustbot assigned SpriteOvO and unassigned kyoto7250 Oct 22, 2024
@SpriteOvO SpriteOvO linked a pull request Oct 27, 2024 that will close this issue
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.

7 participants