1 #![deny(clippy::borrowed_box)]
2 #![allow(clippy::blacklisted_name)]
3 #![allow(unused_variables)]
4 #![allow(dead_code)]
5 
6 use std::fmt::Display;
7 
test1(foo: &mut Box<bool>)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 
test2()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 {
test4(a: &Box<bool>)29     fn test4(a: &Box<bool>);
30 }
31 
32 impl<'a> Test4 for Test3<'a> {
test4(a: &Box<bool>)33     fn test4(a: &Box<bool>) {
34         unimplemented!();
35     }
36 }
37 
38 use std::any::Any;
39 
test5(foo: &mut Box<dyn Any>)40 pub fn test5(foo: &mut Box<dyn Any>) {
41     println!("{:?}", foo)
42 }
43 
test6()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 {
test8(a: &Box<dyn Any>)53     fn test8(a: &Box<dyn Any>);
54 }
55 
56 impl<'a> Test8 for Test7<'a> {
test8(a: &Box<dyn Any>)57     fn test8(a: &Box<dyn Any>) {
58         unimplemented!();
59     }
60 }
61 
test9(foo: &mut Box<dyn Any + Send + Sync>)62 pub fn test9(foo: &mut Box<dyn Any + Send + Sync>) {
63     let _ = foo;
64 }
65 
test10()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 {
test4(a: &Box<dyn Any + 'static>)75     fn test4(a: &Box<dyn Any + 'static>);
76 }
77 
78 impl<'a> Test12 for Test11<'a> {
test4(a: &Box<dyn Any + 'static>)79     fn test4(a: &Box<dyn Any + 'static>) {
80         unimplemented!();
81     }
82 }
83 
test13(boxed_slice: &mut Box<[i32]>)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.
test14(_display: &Box<dyn Display>)95 pub fn test14(_display: &Box<dyn Display>) {}
test15(_display: &Box<dyn Display + Send>)96 pub fn test15(_display: &Box<dyn Display + Send>) {}
test16<'a>(_display: &'a Box<dyn Display + 'a>)97 pub fn test16<'a>(_display: &'a Box<dyn Display + 'a>) {}
98 
test17(_display: &Box<impl Display>)99 pub fn test17(_display: &Box<impl Display>) {}
test18(_display: &Box<impl Display + Send>)100 pub fn test18(_display: &Box<impl Display + Send>) {}
test19<'a>(_display: &'a Box<impl Display + 'a>)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.
test20(_display: &Box<(dyn Display + Send)>)106 pub fn test20(_display: &Box<(dyn Display + Send)>) {}
107 
main()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 }
116