]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/boxed_local.rs
Auto merge of #101969 - reez12g:issue-101306, r=reez12g
[rust.git] / src / tools / clippy / tests / ui / boxed_local.rs
1 #![feature(box_syntax)]
2 #![feature(lint_reasons)]
3 #![allow(
4     clippy::borrowed_box,
5     clippy::needless_pass_by_value,
6     clippy::unused_unit,
7     clippy::redundant_clone,
8     clippy::match_single_binding
9 )]
10 #![warn(clippy::boxed_local)]
11
12 #[derive(Clone)]
13 struct A;
14
15 impl A {
16     fn foo(&self) {}
17 }
18
19 trait Z {
20     fn bar(&self);
21 }
22
23 impl Z for A {
24     fn bar(&self) {
25         //nothing
26     }
27 }
28
29 fn main() {}
30
31 fn ok_box_trait(boxed_trait: &Box<dyn Z>) {
32     let boxed_local = boxed_trait;
33     // done
34 }
35
36 fn warn_call() {
37     let x = box A;
38     x.foo();
39 }
40
41 fn warn_arg(x: Box<A>) {
42     x.foo();
43 }
44
45 fn nowarn_closure_arg() {
46     let x = Some(box A);
47     x.map_or((), |x| take_ref(&x));
48 }
49
50 fn warn_rename_call() {
51     let x = box A;
52
53     let y = x;
54     y.foo(); // via autoderef
55 }
56
57 fn warn_notuse() {
58     let bz = box A;
59 }
60
61 fn warn_pass() {
62     let bz = box A;
63     take_ref(&bz); // via deref coercion
64 }
65
66 fn nowarn_return() -> Box<A> {
67     box A // moved out, "escapes"
68 }
69
70 fn nowarn_move() {
71     let bx = box A;
72     drop(bx) // moved in, "escapes"
73 }
74 fn nowarn_call() {
75     let bx = box A;
76     bx.clone(); // method only available to Box, not via autoderef
77 }
78
79 fn nowarn_pass() {
80     let bx = box A;
81     take_box(&bx); // fn needs &Box
82 }
83
84 fn take_box(x: &Box<A>) {}
85 fn take_ref(x: &A) {}
86
87 fn nowarn_ref_take() {
88     // false positive, should actually warn
89     let x = box A;
90     let y = &x;
91     take_box(y);
92 }
93
94 fn nowarn_match() {
95     let x = box A; // moved into a match
96     match x {
97         y => drop(y),
98     }
99 }
100
101 fn warn_match() {
102     let x = box A;
103     match &x {
104         // not moved
105         y => (),
106     }
107 }
108
109 fn nowarn_large_array() {
110     // should not warn, is large array
111     // and should not be on stack
112     let x = box [1; 10000];
113     match &x {
114         // not moved
115         y => (),
116     }
117 }
118
119 /// ICE regression test
120 pub trait Foo {
121     type Item;
122 }
123
124 impl<'a> Foo for &'a () {
125     type Item = ();
126 }
127
128 pub struct PeekableSeekable<I: Foo> {
129     _peeked: I::Item,
130 }
131
132 pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {}
133
134 /// Regression for #916, #1123
135 ///
136 /// This shouldn't warn for `boxed_local`as the implementation of a trait
137 /// can't change much about the trait definition.
138 trait BoxedAction {
139     fn do_sth(self: Box<Self>);
140 }
141
142 impl BoxedAction for u64 {
143     fn do_sth(self: Box<Self>) {
144         println!("{}", *self)
145     }
146 }
147
148 /// Regression for #1478
149 ///
150 /// This shouldn't warn for `boxed_local`as self itself is a box type.
151 trait MyTrait {
152     fn do_sth(self);
153 }
154
155 impl<T> MyTrait for Box<T> {
156     fn do_sth(self) {}
157 }
158
159 // Issue #3739 - capture in closures
160 mod issue_3739 {
161     use super::A;
162
163     fn consume<T>(_: T) {}
164     fn borrow<T>(_: &T) {}
165
166     fn closure_consume(x: Box<A>) {
167         let _ = move || {
168             consume(x);
169         };
170     }
171
172     fn closure_borrow(x: Box<A>) {
173         let _ = || {
174             borrow(&x);
175         };
176     }
177 }
178
179 /// Issue #5542
180 ///
181 /// This shouldn't warn for `boxed_local` as it is intended to called from non-Rust code.
182 pub extern "C" fn do_not_warn_me(_c_pointer: Box<String>) -> () {}
183
184 #[rustfmt::skip] // Forces rustfmt to not add ABI
185 pub extern fn do_not_warn_me_no_abi(_c_pointer: Box<String>) -> () {}
186
187 // Issue #4804 - default implementation in trait
188 mod issue4804 {
189     trait DefaultTraitImplTest {
190         // don't warn on `self`
191         fn default_impl(self: Box<Self>) -> u32 {
192             5
193         }
194
195         // warn on `x: Box<u32>`
196         fn default_impl_x(self: Box<Self>, x: Box<u32>) -> u32 {
197             4
198         }
199     }
200
201     trait WarnTrait {
202         // warn on `x: Box<u32>`
203         fn foo(x: Box<u32>) {}
204     }
205 }
206
207 fn check_expect(#[expect(clippy::boxed_local)] x: Box<A>) {
208     x.foo();
209 }