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