-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
False positive in map_identity
with lifetimes
#9280
Comments
Does clippy have enough information to be able to ignore identity |
Yes, Clippy has all the information that rustc has.
Also yes, the current implementation is a bit naive IIRC. That causes a false positive. Even though I'm a bit surprised that we need the |
Just ran into this in sharkdp/bat#2755. Asked on community Discord and got a proper explanation:
See: https://discord.com/channels/273534239310479360/1120124565591425034/1170543402870382653 |
I'll give this one a shot. Never messed with the compiler/linter before so wish me luck. @rustbot claim |
Unsurprisingly, I am encountering significant difficulties. I've got a high-level sense of what I'm supposed to check for (don't raise a warning when the input lifetime is unequal to the output lifetime), but honestly not much else. Frankly I feel like I have no idea what I'm doing, which is probably true. I'm not giving up just yet; you know, treating this as a learning opportunity etc. etc, but it will surely take me a lot of time. If anyone else wants to work on this (if you do, you are in all likelihood more competent than me), please go right ahead. |
Hmm, I haven't looked much into this issue, but I would suggest you to look at the following two things:
|
Some lifetimes are erased, it may not be possible to detect |
Run into this with another example: pub fn foo(
input: impl IntoIterator<Item = &'a (A, B)>,
) {
let _ : HashMap<&'a A, &'a B> = input.into_iter().map(|(k, v)| (k, v)).collect();
} Where the input type is |
That bug was fixed in #11792 and I think match ergonomics is unrelated to the lifetime problem here? If I run your code with a newer clippy build (tested nightly 2023-12-03), this indeed no longer emits a warning. |
The bug regarding pub struct Foo;
pub fn foo(x: Option<&(Foo, Foo)>) -> Option<(&Foo, &Foo)> {
x.map(|(a, b)| (a, b))
}
pub fn bar(xs: Vec<&(Foo, Foo)>) -> Vec<(&Foo, &Foo)> {
xs.into_iter().map(|(a, b)| (a, b)).collect()
} Clippy output (clippy 0.1.75 (82e1608 2023-12-21)):
|
@stephaneyfx Your Rust playground link uses the stable channel and stable does not have that fix yet (it takes quite a while for a change made today to actually land on stable, and the date that the playground shows for stable clippy is a bit misleading). Changing the channel to nightly makes the warnings go away, so I presume that it actually is fixed? |
My bad for incorrectly assuming the two-month-old fix was available on stable. I cannot reproduce using nightly with either the playground or my actual code, as you mentioned. Thank you and sorry for the noise. |
Summary
Mapping with an identity closure (
|x| x
) is sometimes required to fix lifetimes.Lint Name
map_identity
Reproducer
I tried this code:
Removing the
map
results in a compile error.I saw this happen:
I expected to see this happen:
No warning, as the
map
cannot be removed without causing a compile error. This happens on both stable and nightly.Version
Additional Labels
@rustbot label +I-suggestion-causes-error
The text was updated successfully, but these errors were encountered: