]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/extra_unused_lifetimes.rs
Auto merge of #101969 - reez12g:issue-101306, r=reez12g
[rust.git] / src / tools / clippy / tests / ui / extra_unused_lifetimes.rs
1 // aux-build:proc_macro_derive.rs
2
3 #![allow(
4     unused,
5     dead_code,
6     clippy::needless_lifetimes,
7     clippy::needless_pass_by_value,
8     clippy::needless_arbitrary_self_type
9 )]
10 #![warn(clippy::extra_unused_lifetimes)]
11
12 #[macro_use]
13 extern crate proc_macro_derive;
14
15 fn empty() {}
16
17 fn used_lt<'a>(x: &'a u8) {}
18
19 fn unused_lt<'a>(x: u8) {}
20
21 fn unused_lt_transitive<'a, 'b: 'a>(x: &'b u8) {
22     // 'a is useless here since it's not directly bound
23 }
24
25 fn lt_return<'a, 'b: 'a>(x: &'b u8) -> &'a u8 {
26     panic!()
27 }
28
29 fn lt_return_only<'a>() -> &'a u8 {
30     panic!()
31 }
32
33 fn unused_lt_blergh<'a>(x: Option<Box<dyn Send + 'a>>) {}
34
35 trait Foo<'a> {
36     fn x(&self, a: &'a u8);
37 }
38
39 impl<'a> Foo<'a> for u8 {
40     fn x(&self, a: &'a u8) {}
41 }
42
43 struct Bar;
44
45 impl Bar {
46     fn x<'a>(&self) {}
47 }
48
49 // test for #489 (used lifetimes in bounds)
50 pub fn parse<'a, I: Iterator<Item = &'a str>>(_it: &mut I) {
51     unimplemented!()
52 }
53 pub fn parse2<'a, I>(_it: &mut I)
54 where
55     I: Iterator<Item = &'a str>,
56 {
57     unimplemented!()
58 }
59
60 struct X {
61     x: u32,
62 }
63
64 impl X {
65     fn self_ref_with_lifetime<'a>(&'a self) {}
66     fn explicit_self_with_lifetime<'a>(self: &'a Self) {}
67 }
68
69 // Methods implementing traits must have matching lifetimes
70 mod issue4291 {
71     trait BadTrait {
72         fn unused_lt<'a>(x: u8) {}
73     }
74
75     impl BadTrait for () {
76         fn unused_lt<'a>(_x: u8) {}
77     }
78 }
79
80 mod issue6437 {
81     pub struct Scalar;
82
83     impl<'a> std::ops::AddAssign<&Scalar> for &mut Scalar {
84         fn add_assign(&mut self, _rhs: &Scalar) {
85             unimplemented!();
86         }
87     }
88
89     impl<'b> Scalar {
90         pub fn something<'c>() -> Self {
91             Self
92         }
93     }
94 }
95
96 // https://github.com/rust-lang/rust-clippy/pull/8737#pullrequestreview-951268213
97 mod first_case {
98     use serde::de::Visitor;
99     pub trait Expected {
100         fn fmt(&self, formatter: &mut std::fmt::Formatter);
101     }
102
103     impl<'de, T> Expected for T
104     where
105         T: Visitor<'de>,
106     {
107         fn fmt(&self, formatter: &mut std::fmt::Formatter) {}
108     }
109 }
110
111 // https://github.com/rust-lang/rust-clippy/pull/8737#pullrequestreview-951268213
112 mod second_case {
113     pub trait Source {
114         fn hey();
115     }
116
117     impl<'a, T: Source + ?Sized + 'a> Source for Box<T> {
118         fn hey() {}
119     }
120 }
121
122 // Should not lint
123 #[derive(ExtraLifetimeDerive)]
124 struct Human<'a> {
125     pub bones: i32,
126     pub name: &'a str,
127 }
128
129 fn main() {}