]> git.lizzy.rs Git - rust.git/blob - tests/ui/unused_self.rs
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[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
47 mod used_self {
48     use std::pin::Pin;
49
50     struct A {
51         x: u8,
52     }
53
54     impl A {
55         fn used_self_move(self) -> u8 {
56             self.x
57         }
58         fn used_self_ref(&self) -> u8 {
59             self.x
60         }
61         fn used_self_mut_ref(&mut self) {
62             self.x += 1
63         }
64         fn used_self_pin_ref(self: Pin<&Self>) -> u8 {
65             self.x
66         }
67         fn used_self_box(self: Box<Self>) -> u8 {
68             self.x
69         }
70         fn used_self_with_other_unused_args(&self, x: u8, y: u8) -> u8 {
71             self.x
72         }
73         fn used_in_nested_closure(&self) -> u8 {
74             let mut a = || -> u8 { self.x };
75             a()
76         }
77
78         #[allow(clippy::collapsible_if)]
79         fn used_self_method_nested_conditions(&self, a: bool, b: bool, c: bool, d: bool) {
80             if a {
81                 if b {
82                     if c {
83                         if d {
84                             self.used_self_ref();
85                         }
86                     }
87                 }
88             }
89         }
90
91         fn foo(&self) -> u32 {
92             let mut sum = 0u32;
93             for i in 0..self.x {
94                 sum += i as u32;
95             }
96             sum
97         }
98
99         fn bar(&mut self, x: u8) -> u32 {
100             let mut y = 0u32;
101             for i in 0..x {
102                 y += self.foo()
103             }
104             y
105         }
106     }
107 }
108
109 mod not_applicable {
110     use std::fmt;
111
112     struct A {}
113
114     impl fmt::Debug for A {
115         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116             write!(f, "A")
117         }
118     }
119
120     impl A {
121         fn method(x: u8, y: u8) {}
122     }
123
124     trait B {
125         fn method(&self) {}
126     }
127 }
128
129 fn main() {}