Skip to content

Instantly share code, notes, and snippets.

@nikomatsakis
Last active March 14, 2018 19:38
Show Gist options
  • Save nikomatsakis/acdeb20b0192e2a1f404d514b3002c59 to your computer and use it in GitHub Desktop.
Save nikomatsakis/acdeb20b0192e2a1f404d514b3002c59 to your computer and use it in GitHub Desktop.
#![warn(single_use_lifetime)]
#![allow(dead_code)]
#![allow(unused_variables)]
fn a<'a>(x: &'a u32) { // Warning: used only once
}
fn b<'a>() -> &'a u32 { // OK: used only in return type
&22
}
fn c<'a>(x: &'a u32) -> &'a u32 { // OK: used twice
&22
}
fn d<'a>() { } // Warning: never used at all
struct Foo<'f> {
data: &'f u32
}
impl<'f> Foo<'f> { // OK for *now* FIXME(#15872)
fn inherent_a(&self) {
}
fn inherent_b<'a>(x: &'a u32) { // Warning for `'a` expected
}
fn inherent_c<'a>() -> &'a u32 { // OK: used only in return type
&22
}
fn inherent_d<'a>(x: &'a u32) -> &'a u32 { // OK: used twice
&22
}
}
impl<'f> Foo<'f> { // OK
fn new(data: &'f u32) -> Self {
Foo { data }
}
}
impl<'f> Iterator for Foo<'f> { // OK: used twice
type Item = &'f u32;
fn next<'g>(&'g mut self) -> Option<Self::Item> { // Warning for `'g`
None
}
}
impl<'f> Foo<'f> { // OK: used twice
fn get(&self) -> &'f u32 {
self.data
}
}
fn main() { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment