]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/unused_self.rs
Rollup merge of #101229 - mgeisler:link-try-operator, r=thomcc
[rust.git] / src / tools / clippy / 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     pub struct D;
58
59     impl D {
60         // shouldn't trigger for public methods
61         pub fn unused_self_move(self) {}
62     }
63 }
64
65 pub use unused_self_allow::D;
66
67 mod used_self {
68     use std::pin::Pin;
69
70     struct A {
71         x: u8,
72     }
73
74     impl A {
75         fn used_self_move(self) -> u8 {
76             self.x
77         }
78         fn used_self_ref(&self) -> u8 {
79             self.x
80         }
81         fn used_self_mut_ref(&mut self) {
82             self.x += 1
83         }
84         fn used_self_pin_ref(self: Pin<&Self>) -> u8 {
85             self.x
86         }
87         fn used_self_box(self: Box<Self>) -> u8 {
88             self.x
89         }
90         fn used_self_with_other_unused_args(&self, x: u8, y: u8) -> u8 {
91             self.x
92         }
93         fn used_in_nested_closure(&self) -> u8 {
94             let mut a = || -> u8 { self.x };
95             a()
96         }
97
98         #[allow(clippy::collapsible_if)]
99         fn used_self_method_nested_conditions(&self, a: bool, b: bool, c: bool, d: bool) {
100             if a {
101                 if b {
102                     if c {
103                         if d {
104                             self.used_self_ref();
105                         }
106                     }
107                 }
108             }
109         }
110
111         fn foo(&self) -> u32 {
112             let mut sum = 0u32;
113             for i in 0..self.x {
114                 sum += i as u32;
115             }
116             sum
117         }
118
119         fn bar(&mut self, x: u8) -> u32 {
120             let mut y = 0u32;
121             for i in 0..x {
122                 y += self.foo()
123             }
124             y
125         }
126     }
127 }
128
129 mod not_applicable {
130     use std::fmt;
131
132     struct A;
133
134     impl fmt::Debug for A {
135         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136             write!(f, "A")
137         }
138     }
139
140     impl A {
141         fn method(x: u8, y: u8) {}
142     }
143
144     trait B {
145         fn method(&self) {}
146     }
147 }
148
149 fn main() {}