rust-clippy
rust-clippy copied to clipboard
`manual_inspect` suggestion breaks mutating code
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
@rustbot claim
Hit this exact issue (a d the other linked issue) in this code
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
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.
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.
@rustbot claim
@rustbot release-assignment
I apologize for not having the time to continue this work in the near future.