]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrow_box.rs
Rename "blacklisted name" to "disallowed name" throughout
[rust.git] / tests / ui / borrow_box.rs
1 #![deny(clippy::borrowed_box)]
2 #![allow(clippy::disallowed_name)]
3 #![allow(unused_variables)]
4 #![allow(dead_code)]
5
6 use std::fmt::Display;
7
8 pub fn test1(foo: &mut Box<bool>) {
9     // Although this function could be changed to "&mut bool",
10     // avoiding the Box, mutable references to boxes are not
11     // flagged by this lint.
12     //
13     // This omission is intentional: By passing a mutable Box,
14     // the memory location of the pointed-to object could be
15     // modified. By passing a mutable reference, the contents
16     // could change, but not the location.
17     println!("{:?}", foo)
18 }
19
20 pub fn test2() {
21     let foo: &Box<bool>;
22 }
23
24 struct Test3<'a> {
25     foo: &'a Box<bool>,
26 }
27
28 trait Test4 {
29     fn test4(a: &Box<bool>);
30 }
31
32 impl<'a> Test4 for Test3<'a> {
33     fn test4(a: &Box<bool>) {
34         unimplemented!();
35     }
36 }
37
38 use std::any::Any;
39
40 pub fn test5(foo: &mut Box<dyn Any>) {
41     println!("{:?}", foo)
42 }
43
44 pub fn test6() {
45     let foo: &Box<dyn Any>;
46 }
47
48 struct Test7<'a> {
49     foo: &'a Box<dyn Any>,
50 }
51
52 trait Test8 {
53     fn test8(a: &Box<dyn Any>);
54 }
55
56 impl<'a> Test8 for Test7<'a> {
57     fn test8(a: &Box<dyn Any>) {
58         unimplemented!();
59     }
60 }
61
62 pub fn test9(foo: &mut Box<dyn Any + Send + Sync>) {
63     let _ = foo;
64 }
65
66 pub fn test10() {
67     let foo: &Box<dyn Any + Send + 'static>;
68 }
69
70 struct Test11<'a> {
71     foo: &'a Box<dyn Any + Send>,
72 }
73
74 trait Test12 {
75     fn test4(a: &Box<dyn Any + 'static>);
76 }
77
78 impl<'a> Test12 for Test11<'a> {
79     fn test4(a: &Box<dyn Any + 'static>) {
80         unimplemented!();
81     }
82 }
83
84 pub fn test13(boxed_slice: &mut Box<[i32]>) {
85     // Unconditionally replaces the box pointer.
86     //
87     // This cannot be accomplished if "&mut [i32]" is passed,
88     // and provides a test case where passing a reference to
89     // a Box is valid.
90     let mut data = vec![12];
91     *boxed_slice = data.into_boxed_slice();
92 }
93
94 // The suggestion should include proper parentheses to avoid a syntax error.
95 pub fn test14(_display: &Box<dyn Display>) {}
96 pub fn test15(_display: &Box<dyn Display + Send>) {}
97 pub fn test16<'a>(_display: &'a Box<dyn Display + 'a>) {}
98
99 pub fn test17(_display: &Box<impl Display>) {}
100 pub fn test18(_display: &Box<impl Display + Send>) {}
101 pub fn test19<'a>(_display: &'a Box<impl Display + 'a>) {}
102
103 // This exists only to check what happens when parentheses are already present.
104 // Even though the current implementation doesn't put extra parentheses,
105 // it's fine that unnecessary parentheses appear in the future for some reason.
106 pub fn test20(_display: &Box<(dyn Display + Send)>) {}
107
108 fn main() {
109     test1(&mut Box::new(false));
110     test2();
111     test5(&mut (Box::new(false) as Box<dyn Any>));
112     test6();
113     test9(&mut (Box::new(false) as Box<dyn Any + Send + Sync>));
114     test10();
115 }