2018-10-06 18:18:06 +02:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#![warn(clippy::needless_lifetimes, clippy::extra_unused_lifetimes)]
|
|
|
|
#![allow(dead_code, clippy::needless_pass_by_value, clippy::trivially_copy_pass_by_ref)]
|
2016-01-29 22:18:14 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) {} // no error, same lifetime on two params
|
2015-08-12 13:16:09 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) {} // no error, static involved
|
2015-08-12 13:16:09 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn mut_and_static_input(_x: &mut u8, _y: &'static str) {}
|
2015-08-15 09:36:02 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 {
|
|
|
|
x
|
|
|
|
}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 {
|
|
|
|
x
|
|
|
|
} // no error, multiple input refs
|
2015-08-12 13:16:09 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
|
|
|
|
x
|
|
|
|
} // no error, multiple input refs
|
2015-08-12 13:16:09 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 {
|
|
|
|
x
|
|
|
|
} // no error, static involved
|
2015-08-12 13:16:09 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
|
|
|
|
Ok(x)
|
|
|
|
} // no error
|
2015-08-12 13:16:09 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
|
|
|
|
x.unwrap()
|
|
|
|
} // no error, two input refs
|
2015-08-12 13:16:09 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> {
|
|
|
|
Ok(x)
|
|
|
|
}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-08-30 09:57:52 +02:00
|
|
|
// where clause, but without lifetimes
|
2018-12-09 23:26:16 +01:00
|
|
|
fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()>
|
|
|
|
where
|
|
|
|
T: Copy,
|
|
|
|
{
|
|
|
|
Ok(x)
|
|
|
|
}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-08-13 06:43:25 +02:00
|
|
|
type Ref<'r> = &'r u8;
|
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {} // no error, same lifetime on two params
|
2015-08-13 06:43:25 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {} // no error, bounded lifetime
|
2015-08-13 17:24:47 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8)
|
|
|
|
where
|
|
|
|
'b: 'a,
|
|
|
|
{
|
|
|
|
} // no error, bounded lifetime
|
2015-08-30 09:57:52 +02:00
|
|
|
|
2015-08-31 11:11:51 +02:00
|
|
|
struct Lt<'a, I: 'static> {
|
2018-12-09 23:26:16 +01:00
|
|
|
x: &'a I,
|
2015-08-31 11:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
|
2018-12-09 23:26:16 +01:00
|
|
|
where
|
|
|
|
F: Fn(Lt<'a, I>) -> Lt<'a, I>, // no error, fn bound references 'a
|
|
|
|
{
|
|
|
|
unreachable!()
|
|
|
|
}
|
2015-08-31 11:11:51 +02:00
|
|
|
|
2017-02-08 14:58:07 +01:00
|
|
|
fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
|
2018-12-09 23:26:16 +01:00
|
|
|
where
|
|
|
|
for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
|
|
|
|
{
|
|
|
|
unreachable!()
|
|
|
|
}
|
2015-08-31 11:11:51 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) {
|
|
|
|
// no error, see below
|
2018-06-19 17:12:17 +02:00
|
|
|
f(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_bound_3_cannot_elide() {
|
|
|
|
let x = 42;
|
|
|
|
let p = &x;
|
|
|
|
let mut q = &x;
|
|
|
|
fn_bound_3(p, |y| q = y); // this will fail if we elides lifetimes of `fn_bound_3`
|
|
|
|
}
|
2018-06-19 15:25:38 +02:00
|
|
|
|
2018-06-19 17:12:17 +02:00
|
|
|
// no error, multiple input refs
|
|
|
|
fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a () {
|
2018-12-09 23:26:16 +01:00
|
|
|
if cond {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
f()
|
|
|
|
}
|
2018-06-19 17:12:17 +02:00
|
|
|
}
|
2018-06-19 15:25:38 +02:00
|
|
|
|
2015-08-12 13:16:09 +02:00
|
|
|
struct X {
|
|
|
|
x: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl X {
|
2018-12-09 23:26:16 +01:00
|
|
|
fn self_and_out<'s>(&'s self) -> &'s u8 {
|
|
|
|
&self.x
|
|
|
|
}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
|
|
|
|
&self.x
|
|
|
|
} // no error, multiple input refs
|
2015-08-12 13:16:09 +02:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn self_and_same_in<'s>(&'s self, _x: &'s u8) {} // no error, same lifetimes on two params
|
2015-08-12 13:16:09 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 17:40:45 +02:00
|
|
|
struct Foo<'a>(&'a u8);
|
|
|
|
|
|
|
|
impl<'a> Foo<'a> {
|
|
|
|
fn self_shared_lifetime(&self, _: &'a u8) {} // no error, lifetime 'a not defined in method
|
|
|
|
fn self_bound_lifetime<'b: 'a>(&self, _: &'b u8) {} // no error, bounds exist
|
|
|
|
}
|
2015-08-12 13:16:09 +02:00
|
|
|
|
2015-09-30 16:40:54 +02:00
|
|
|
fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-11-11 00:26:22 +01:00
|
|
|
|
|
|
|
// no warning, two input lifetimes (named on the reference, anonymous on Foo)
|
2018-12-09 23:26:16 +01:00
|
|
|
fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-11-11 00:26:22 +01:00
|
|
|
|
|
|
|
// no warning, two input lifetimes (anonymous on the reference, named on Foo)
|
2018-12-09 23:26:16 +01:00
|
|
|
fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-11-11 00:26:22 +01:00
|
|
|
|
|
|
|
// no warning, two input lifetimes
|
2018-12-09 23:26:16 +01:00
|
|
|
fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-11-11 00:26:22 +01:00
|
|
|
|
|
|
|
trait WithLifetime<'a> {}
|
2016-01-29 22:18:14 +01:00
|
|
|
|
2015-11-11 00:26:22 +01:00
|
|
|
type WithLifetimeAlias<'a> = WithLifetime<'a>;
|
|
|
|
|
|
|
|
// should not warn because it won't build without the lifetime
|
2018-12-09 23:26:16 +01:00
|
|
|
fn trait_obj_elided<'a>(_arg: &'a WithLifetime) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-11-11 00:26:22 +01:00
|
|
|
|
|
|
|
// this should warn because there is no lifetime on Drop, so this would be
|
|
|
|
// unambiguous if we elided the lifetime
|
2018-12-09 23:26:16 +01:00
|
|
|
fn trait_obj_elided2<'a>(_arg: &'a Drop) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-11-11 00:26:22 +01:00
|
|
|
|
2015-12-04 15:56:25 +01:00
|
|
|
type FooAlias<'a> = Foo<'a>;
|
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-12-04 15:56:25 +01:00
|
|
|
|
|
|
|
// no warning, two input lifetimes (named on the reference, anonymous on Foo)
|
2018-12-09 23:26:16 +01:00
|
|
|
fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-12-04 15:56:25 +01:00
|
|
|
|
|
|
|
// no warning, two input lifetimes (anonymous on the reference, named on Foo)
|
2018-12-09 23:26:16 +01:00
|
|
|
fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-12-04 15:56:25 +01:00
|
|
|
|
|
|
|
// no warning, two input lifetimes
|
2018-12-09 23:26:16 +01:00
|
|
|
fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-12-04 15:56:25 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn named_input_elided_output<'a>(_arg: &'a str) -> &str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-01-17 17:53:41 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn elided_input_named_output<'a>(_arg: &str) -> &'a str {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-01-17 17:53:41 +01:00
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-01-29 22:18:14 +01:00
|
|
|
|
2017-04-11 14:09:58 +02:00
|
|
|
// don't warn on these, see #292
|
2018-12-09 23:26:16 +01:00
|
|
|
fn trait_bound_bug<'a, T: WithLifetime<'a>>() {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2017-04-11 14:09:58 +02:00
|
|
|
|
|
|
|
// #740
|
|
|
|
struct Test {
|
|
|
|
vec: Vec<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test {
|
|
|
|
fn iter<'a>(&'a self) -> Box<Iterator<Item = usize> + 'a> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait LintContext<'a> {}
|
|
|
|
|
2017-04-11 14:34:39 +02:00
|
|
|
fn f<'a, T: LintContext<'a>>(_: &T) {}
|
2017-04-11 14:09:58 +02:00
|
|
|
|
|
|
|
fn test<'a>(x: &'a [u8]) -> u8 {
|
|
|
|
let y: &'a u8 = &x[5];
|
|
|
|
*y
|
|
|
|
}
|
|
|
|
|
2018-10-08 10:43:13 +02:00
|
|
|
// #3284 - Give a hint regarding lifetime in return type
|
|
|
|
|
2018-12-09 23:26:16 +01:00
|
|
|
struct Cow<'a> {
|
|
|
|
x: &'a str,
|
2015-08-12 13:16:09 +02:00
|
|
|
}
|
2018-12-09 23:26:16 +01:00
|
|
|
fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|