]> git.lizzy.rs Git - rust.git/blob - tests/ui/unused_self.rs
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[rust.git] / tests / ui / unused_self.rs
1 #![warn(clippy::unused_self)]
2 #![allow(clippy::boxed_local, clippy::fn_params_excessive_bools)]
3
4 mod unused_self {
5     use std::pin::Pin;
6     use std::sync::{Arc, Mutex};
7
8     struct A {}
9
10     impl A {
11         fn unused_self_move(self) {}
12         fn unused_self_ref(&self) {}
13         fn unused_self_mut_ref(&mut self) {}
14         fn unused_self_pin_ref(self: Pin<&Self>) {}
15         fn unused_self_pin_mut_ref(self: Pin<&mut Self>) {}
16         fn unused_self_pin_nested(self: Pin<Arc<Self>>) {}
17         fn unused_self_box(self: Box<Self>) {}
18         fn unused_with_other_used_args(&self, x: u8, y: u8) -> u8 {
19             x + y
20         }
21         fn unused_self_class_method(&self) {
22             Self::static_method();
23         }
24
25         fn static_method() {}
26     }
27 }
28
29 mod unused_self_allow {
30     struct A {}
31
32     impl A {
33         // shouldn't trigger
34         #[allow(clippy::unused_self)]
35         fn unused_self_move(self) {}
36     }
37
38     struct B {}
39
40     // shouldn't trigger
41     #[allow(clippy::unused_self)]
42     impl B {
43         fn unused_self_move(self) {}
44     }
45
46     struct C {}
47
48     #[allow(clippy::unused_self)]
49     impl C {
50         #[warn(clippy::unused_self)]
51         fn some_fn((): ()) {}
52
53         // shouldn't trigger
54         fn unused_self_move(self) {}
55     }
56 }
57
58 mod used_self {
59     use std::pin::Pin;
60
61     struct A {
62         x: u8,
63     }
64
65     impl A {
66         fn used_self_move(self) -> u8 {
67             self.x
68         }
69         fn used_self_ref(&self) -> u8 {
70             self.x
71         }
72         fn used_self_mut_ref(&mut self) {
73             self.x += 1
74         }
75         fn used_self_pin_ref(self: Pin<&Self>) -> u8 {
76             self.x
77         }
78         fn used_self_box(self: Box<Self>) -> u8 {
79             self.x
80         }
81         fn used_self_with_other_unused_args(&self, x: u8, y: u8) -> u8 {
82             self.x
83         }
84         fn used_in_nested_closure(&self) -> u8 {
85             let mut a = || -> u8 { self.x };
86             a()
87         }
88
89         #[allow(clippy::collapsible_if)]
90         fn used_self_method_nested_conditions(&self, a: bool, b: bool, c: bool, d: bool) {
91             if a {
92                 if b {
93                     if c {
94                         if d {
95                             self.used_self_ref();
96                         }
97                     }
98                 }
99             }
100         }
101
102         fn foo(&self) -> u32 {
103             let mut sum = 0u32;
104             for i in 0..self.x {
105                 sum += i as u32;
106             }
107             sum
108         }
109
110         fn bar(&mut self, x: u8) -> u32 {
111             let mut y = 0u32;
112             for i in 0..x {
113                 y += self.foo()
114             }
115             y
116         }
117     }
118 }
119
120 mod not_applicable {
121     use std::fmt;
122
123     struct A {}
124
125     impl fmt::Debug for A {
126         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127             write!(f, "A")
128         }
129     }
130
131     impl A {
132         fn method(x: u8, y: u8) {}
133     }
134
135     trait B {
136         fn method(&self) {}
137     }
138 }
139
140 fn main() {}