-
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
Incorrect needless_lifetimes warning for impl Trait based code #2944
Comments
I think I have also encountered this issue, with a more minimal example: struct Context {
x: i32,
}
impl Context {
fn get_x<'a>(&'a self) -> impl Fn() -> &'a i32 {
move || &self.x
}
} With a little guidance, I'm happy to make a PR to fix the issue. |
The fix needs to be made in the rust-clippy/clippy_lints/src/lifetimes.rs Lines 150 to 156 in 45d24fd
function. This function assumes, that elision is possible, if rust-clippy/clippy_lints/src/lifetimes.rs Line 159 in 45d24fd
This is wrong in this case. This lint is one of the oldest lints Clippy has (#140, way before my time), and was only touched once (#1672) since then. So I'm not really sure how exactly it works and can only point you to the right place 😉 |
Same issue here while hacking on glsl. Minimal reproducible code I was able to get: use nom::IResult;
use nom::error::VerboseError;
use std::ops::Fn;
pub type ParserResult<'a, O> = IResult<&'a str, O, VerboseError<&'a str>>;
pub fn keyword<'a>(_: &'a str) -> impl Fn(&'a str) -> ParserResult<'a, &'a str> {
|_| unimplemented!()
} With |
I'm taking a stab at this one. |
There are two distinct cases in this issue. case 1, the original one in the description: impl MyMap {
fn iter<'a>(&'a self, key: u32) -> Option<impl Iterator<Item = impl AsRef<str> + 'a>> {
self.inner.get(&key).map(|set| set.iter())
}
} In this case, the lint is actually correct. Removing the lifetime notations produces an error, but the compiler error has improved:
So the following version with anonymous lifetime compiles fine. impl MyMap {
fn iter(&self, key: u32) -> Option<impl Iterator<Item = impl AsRef<str> + '_>> {
self.inner.get(&key).map(|set| set.iter())
}
} Since the lint is not making any wrong suggestion here, I would leave it as is. The compiler error will help the user to find the correct elided syntax. case 2: the next examples commented in the issue boil down to this: fn get_x<'a>(i32_pointer: &'a i32) -> impl Fn() -> &'a i32 {
move || i32_pointer
} Lifetime elision applies to three constructs: function items, function pointers, and closure trait bounds. This case has a nested elision candidate in One solution to avoid FPs here is to bail out if a function pointer or a closure trait bound is found in the function signature. That might introduce a few FNs though. |
I believe I'm getting this as well for:
Using |
@andrewbanchich I think your case looks more like #4746 as you can't elide the lifetime here because it's not related to |
@ebroto That makes sense. Thanks! |
Given the following code:
Playground
clippy reports the following:
But if you take its suggestion and remove
'a
fromiter()
, you get the following compiler error:It's possible this is a dupe of an existing issue but a cursory search revealed a few old (and closed)
impl Trait
related bugs, and I didn't find anything current.The text was updated successfully, but these errors were encountered: