|
| 1 | +#![deny(clippy::borrowed_box)] |
| 2 | +#![allow(dead_code, unused_variables)] |
| 3 | +#![allow( |
| 4 | + clippy::uninlined_format_args, |
| 5 | + clippy::disallowed_names, |
| 6 | + clippy::needless_pass_by_ref_mut |
| 7 | +)] |
| 8 | + |
| 9 | +use std::fmt::Display; |
| 10 | + |
| 11 | +pub fn test1(foo: &mut Box<bool>) { |
| 12 | + // Although this function could be changed to "&mut bool", |
| 13 | + // avoiding the Box, mutable references to boxes are not |
| 14 | + // flagged by this lint. |
| 15 | + // |
| 16 | + // This omission is intentional: By passing a mutable Box, |
| 17 | + // the memory location of the pointed-to object could be |
| 18 | + // modified. By passing a mutable reference, the contents |
| 19 | + // could change, but not the location. |
| 20 | + println!("{:?}", foo) |
| 21 | +} |
| 22 | + |
| 23 | +pub fn test2() { |
| 24 | + let foo: &bool; |
| 25 | + //~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 26 | +} |
| 27 | + |
| 28 | +struct Test3<'a> { |
| 29 | + foo: &'a bool, |
| 30 | + //~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 31 | +} |
| 32 | + |
| 33 | +trait Test4 { |
| 34 | + fn test4(a: &bool); |
| 35 | + //~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 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: &dyn Display) {} |
| 96 | +//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 97 | +pub fn test15(_display: &(dyn Display + Send)) {} |
| 98 | +//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 99 | +pub fn test16<'a>(_display: &'a (dyn Display + 'a)) {} |
| 100 | +//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 101 | + |
| 102 | +pub fn test17(_display: &impl Display) {} |
| 103 | +//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 104 | +pub fn test18(_display: &(impl Display + Send)) {} |
| 105 | +//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 106 | +pub fn test19<'a>(_display: &'a (impl Display + 'a)) {} |
| 107 | +//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 108 | + |
| 109 | +// This exists only to check what happens when parentheses are already present. |
| 110 | +// Even though the current implementation doesn't put extra parentheses, |
| 111 | +// it's fine that unnecessary parentheses appear in the future for some reason. |
| 112 | +pub fn test20(_display: &(dyn Display + Send)) {} |
| 113 | +//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T` |
| 114 | + |
| 115 | +#[allow(clippy::borrowed_box)] |
| 116 | +trait Trait { |
| 117 | + fn f(b: &Box<bool>); |
| 118 | +} |
| 119 | + |
| 120 | +// Trait impls are not linted |
| 121 | +impl Trait for () { |
| 122 | + fn f(_: &Box<bool>) {} |
| 123 | +} |
| 124 | + |
| 125 | +fn main() { |
| 126 | + test1(&mut Box::new(false)); |
| 127 | + test2(); |
| 128 | + test5(&mut (Box::new(false) as Box<dyn Any>)); |
| 129 | + test6(); |
| 130 | + test9(&mut (Box::new(false) as Box<dyn Any + Send + Sync>)); |
| 131 | + test10(); |
| 132 | +} |
0 commit comments