1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #![warn(clippy::needless_lifetimes, clippy::extra_unused_lifetimes)]
11 #![allow(dead_code, clippy::needless_pass_by_value, clippy::trivially_copy_pass_by_ref)]
13 fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
15 fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
17 fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) {} // no error, same lifetime on two params
19 fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) {} // no error, static involved
21 fn mut_and_static_input(_x: &mut u8, _y: &'static str) {}
23 fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 {
27 fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 {
29 } // no error, multiple input refs
31 fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
33 } // no error, multiple input refs
35 fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 {
37 } // no error, static involved
39 fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
43 fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
45 } // no error, two input refs
47 fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> {
51 // where clause, but without lifetimes
52 fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()>
59 type Ref<'r> = &'r u8;
61 fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {} // no error, same lifetime on two params
63 fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
65 fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {} // no error, bounded lifetime
67 fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8)
71 } // no error, bounded lifetime
73 struct Lt<'a, I: 'static> {
77 fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
79 F: Fn(Lt<'a, I>) -> Lt<'a, I>, // no error, fn bound references 'a
84 fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
86 for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
91 fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) {
92 // no error, see below
96 fn fn_bound_3_cannot_elide() {
100 fn_bound_3(p, |y| q = y); // this will fail if we elides lifetimes of `fn_bound_3`
103 // no error, multiple input refs
104 fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a () {
117 fn self_and_out<'s>(&'s self) -> &'s u8 {
121 fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
123 } // no error, multiple input refs
125 fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
127 fn self_and_same_in<'s>(&'s self, _x: &'s u8) {} // no error, same lifetimes on two params
130 struct Foo<'a>(&'a u8);
133 fn self_shared_lifetime(&self, _: &'a u8) {} // no error, lifetime 'a not defined in method
134 fn self_bound_lifetime<'b: 'a>(&self, _: &'b u8) {} // no error, bounds exist
137 fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 {
141 fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
145 // no warning, two input lifetimes (named on the reference, anonymous on Foo)
146 fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str {
150 // no warning, two input lifetimes (anonymous on the reference, named on Foo)
151 fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
155 // no warning, two input lifetimes
156 fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
160 trait WithLifetime<'a> {}
162 type WithLifetimeAlias<'a> = WithLifetime<'a>;
164 // should not warn because it won't build without the lifetime
165 fn trait_obj_elided<'a>(_arg: &'a WithLifetime) -> &'a str {
169 // this should warn because there is no lifetime on Drop, so this would be
170 // unambiguous if we elided the lifetime
171 fn trait_obj_elided2<'a>(_arg: &'a Drop) -> &'a str {
175 type FooAlias<'a> = Foo<'a>;
177 fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
181 // no warning, two input lifetimes (named on the reference, anonymous on Foo)
182 fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str {
186 // no warning, two input lifetimes (anonymous on the reference, named on Foo)
187 fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
191 // no warning, two input lifetimes
192 fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
196 fn named_input_elided_output<'a>(_arg: &'a str) -> &str {
200 fn elided_input_named_output<'a>(_arg: &str) -> &'a str {
204 fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) {
207 fn trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T) {
211 // don't warn on these, see #292
212 fn trait_bound_bug<'a, T: WithLifetime<'a>>() {
222 fn iter<'a>(&'a self) -> Box<Iterator<Item = usize> + 'a> {
227 trait LintContext<'a> {}
229 fn f<'a, T: LintContext<'a>>(_: &T) {}
231 fn test<'a>(x: &'a [u8]) -> u8 {
232 let y: &'a u8 = &x[5];
236 // #3284 - Give a hint regarding lifetime in return type
241 fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {