Skip to content
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

Warn when elided lifetime is shorter than it could have been. #42287

Closed
matklad opened this Issue May 28, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@matklad
Copy link
Member

matklad commented May 28, 2017

Hi! Several times I've faced this interesting case of lifetime miselision:

struct Foo<'a> { bar: &'a Bar}

impl<'a> Foo<'a> {
  fn get_bar(&self) -> & /* 'a */ Bar { self.bar }
}

http://play.integer32.com/?gist=3aba0526aff729bb9d62483a10732826&version=undefined

Here, we want to use 'a lifetime for the returned Bar reference, but lifetime elision gives use lifetime of the self reference. The problem here is that code with elided lifetimes compiles flawlessly by itself, but may fail at the call site, and this is tremendously confusing, because you get a misleading compiler error in the completely wrong place! I've hit this issues several times in practice (the latest one).

I wonder if we can warn about this? That is, we issue a warning if a lifetime in a returned position is elided, but the lifetime of the returned value from the function can actually be larger (alternatively, if the actual lifetime is among the input lifetimes, and is not the same as the one inferred by elision). The fix for warning would be to specify lifetimes explicitly (I suppose that "I did mean to use a shorter lifetime here" is a rather rare use-case).

@Mark-Simulacrum

This comment has been minimized.

Copy link
Member

Mark-Simulacrum commented Jun 23, 2017

I don't think this is going to be a good fit for Rust, since I think there could be false positives. I'm going to close this issue -- feel free to file either an RFC or ask Clippy if they'd like something like this.

@leodasvacas

This comment has been minimized.

Copy link
Contributor

leodasvacas commented Mar 2, 2018

@Mark-Simulacrum Any case in which this is a false positive is special and should be documented with explicit lifetimes anyhow, this seems like a very good lint for rustc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.