-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Wrong suggestion for clippy::map_clone
#12612
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
Comments
oh, I think this may be a dupe of #12560 |
It seems to have been fixed in #12535. I'll add regression tests and then we can close it. |
@GuillaumeGomez thanks for linking the PR! Just to make sure this doesn't go under the radar: to me, the more important problem is the fact that it recommends the wrong fix. #![allow(dead_code)]
#![allow(clippy::needless_borrow)]
pub struct S1 {
v1: Option<S2>,
}
impl S1 {
fn v2(&self) -> Option<String> {
self.v1().map(|v1| String::clone(&v1.v2))
}
fn v1(&self) -> Option<&S2> {
match &self.v1 {
None => None,
Some(v) => Some(&v),
}
}
}
pub struct S2 {
v2: String,
} In this case, it returns
but applying the suggestion generates wrong code that doesn't compile. I also think this is already fixed in nightly, since I can't repro it on playground nightly. But I couldn't find the corresponding issue so I'm just trying to make sure I'm not misunderstanding. |
Let me add a check for this case as well. |
Thanks! |
This case was fixed as well. I added it as part of #12647. |
Fixed with #12282. |
Summary
For this repro, I see clippy suggesting a change that leads to broken code. In fact, I'm even convinced the lint should not even match for this particular case. I believe this started happening on 1.77.
According to playground, it also repros in beta, but not in nightly, so maybe it's already fixed? If so, apologies for wasting your time, but I couldn't find other closed bug submissions for this bug.
Reproducer
I tried this code:
I expected to see this happen:
Instead, this happened:
Version
Additional Labels
@rustbot label +I-suggestion-causes-error
The text was updated successfully, but these errors were encountered: