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