rust-lang / rust-lang/rust-clippy

`manual_inspect` suggestion breaks mutating code

Open
#13,185 8 comments 21 reactions 1 assignee View on GitHub

@willwang-io is already working on this.

Since Sep 11, 2026.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.