]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrow_box.rs
iterate List by value
[rust.git] / tests / ui / borrow_box.rs
1 #![deny(clippy::borrowed_box)]
2 #![allow(clippy::blacklisted_name)]
3 #![allow(unused_variables)]
4 #![allow(dead_code)]
5
6 pub fn test1(foo: &mut Box<bool>) {
7     // Although this function could be changed to "&mut bool",
8     // avoiding the Box, mutable references to boxes are not
9     // flagged by this lint.
10     //
11     // This omission is intentional: By passing a mutable Box,
12     // the memory location of the pointed-to object could be
13     // modified. By passing a mutable reference, the contents
14     // could change, but not the location.
15     println!("{:?}", foo)
16 }
17
18 pub fn test2() {
19     let foo: &Box<bool>;
20 }
21
22 struct Test3<'a> {
23     foo: &'a Box<bool>,
24 }
25
26 trait Test4 {
27     fn test4(a: &Box<bool>);
28 }
29
30 impl<'a> Test4 for Test3<'a> {
31     fn test4(a: &Box<bool>) {
32         unimplemented!();
33     }
34 }
35
36 use std::any::Any;
37
38 pub fn test5(foo: &mut Box<dyn Any>) {
39     println!("{:?}", foo)
40 }
41
42 pub fn test6() {
43     let foo: &Box<dyn Any>;
44 }
45
46 struct Test7<'a> {
47     foo: &'a Box<dyn Any>,
48 }
49
50 trait Test8 {
51     fn test8(a: &Box<dyn Any>);
52 }
53
54 impl<'a> Test8 for Test7<'a> {
55     fn test8(a: &Box<dyn Any>) {
56         unimplemented!();
57     }
58 }
59
60 pub fn test9(foo: &mut Box<dyn Any + Send + Sync>) {
61     let _ = foo;
62 }
63
64 pub fn test10() {
65     let foo: &Box<dyn Any + Send + 'static>;
66 }
67
68 struct Test11<'a> {
69     foo: &'a Box<dyn Any + Send>,
70 }
71
72 trait Test12 {
73     fn test4(a: &Box<dyn Any + 'static>);
74 }
75
76 impl<'a> Test12 for Test11<'a> {
77     fn test4(a: &Box<dyn Any + 'static>) {
78         unimplemented!();
79     }
80 }
81
82 pub fn test13(boxed_slice: &mut Box<[i32]>) {
83     // Unconditionally replaces the box pointer.
84     //
85     // This cannot be accomplished if "&mut [i32]" is passed,
86     // and provides a test case where passing a reference to
87     // a Box is valid.
88     let mut data = vec![12];
89     *boxed_slice = data.into_boxed_slice();
90 }
91
92 fn main() {
93     test1(&mut Box::new(false));
94     test2();
95     test5(&mut (Box::new(false) as Box<dyn Any>));
96     test6();
97     test9(&mut (Box::new(false) as Box<dyn Any + Send + Sync>));
98     test10();
99 }