2018-07-28 10:34:52 -05:00
|
|
|
#![deny(clippy::borrowed_box)]
|
2022-10-06 02:44:38 -05:00
|
|
|
#![allow(dead_code, unused_variables)]
|
2023-07-17 03:19:29 -05:00
|
|
|
#![allow(
|
|
|
|
clippy::uninlined_format_args,
|
|
|
|
clippy::disallowed_names,
|
2024-10-03 09:32:51 -05:00
|
|
|
clippy::needless_pass_by_ref_mut,
|
|
|
|
clippy::needless_lifetimes
|
2023-07-17 03:19:29 -05:00
|
|
|
)]
|
2017-06-10 22:50:57 -05:00
|
|
|
|
2020-11-05 07:29:48 -06:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
2017-06-10 22:50:57 -05:00
|
|
|
pub fn test1(foo: &mut Box<bool>) {
|
2020-04-19 09:49:12 -05:00
|
|
|
// 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.
|
2017-06-10 22:50:57 -05:00
|
|
|
println!("{:?}", foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test2() {
|
|
|
|
let foo: &Box<bool>;
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2017-06-10 22:50:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Test3<'a> {
|
2018-12-09 16:26:16 -06:00
|
|
|
foo: &'a Box<bool>,
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2017-06-10 22:50:57 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 07:44:08 -05:00
|
|
|
trait Test4 {
|
|
|
|
fn test4(a: &Box<bool>);
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2017-06-12 07:44:08 -05:00
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:56 -05:00
|
|
|
use std::any::Any;
|
|
|
|
|
2019-05-30 01:23:47 -05:00
|
|
|
pub fn test5(foo: &mut Box<dyn Any>) {
|
2017-08-14 15:04:56 -05:00
|
|
|
println!("{:?}", foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test6() {
|
2019-05-30 01:23:47 -05:00
|
|
|
let foo: &Box<dyn Any>;
|
2017-08-14 15:04:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Test7<'a> {
|
2019-05-30 01:23:47 -05:00
|
|
|
foo: &'a Box<dyn Any>,
|
2017-08-14 15:04:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Test8 {
|
2019-05-30 01:23:47 -05:00
|
|
|
fn test8(a: &Box<dyn Any>);
|
2017-08-14 15:04:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Test8 for Test7<'a> {
|
2019-05-30 01:23:47 -05:00
|
|
|
fn test8(a: &Box<dyn Any>) {
|
2017-08-14 15:04:56 -05:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 01:23:47 -05:00
|
|
|
pub fn test9(foo: &mut Box<dyn Any + Send + Sync>) {
|
2017-08-14 15:04:56 -05:00
|
|
|
let _ = foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test10() {
|
2019-05-30 01:23:47 -05:00
|
|
|
let foo: &Box<dyn Any + Send + 'static>;
|
2017-08-14 15:04:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Test11<'a> {
|
2019-05-30 01:23:47 -05:00
|
|
|
foo: &'a Box<dyn Any + Send>,
|
2017-08-14 15:04:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Test12 {
|
2019-05-30 01:23:47 -05:00
|
|
|
fn test4(a: &Box<dyn Any + 'static>);
|
2017-08-14 15:04:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Test12 for Test11<'a> {
|
2019-05-30 01:23:47 -05:00
|
|
|
fn test4(a: &Box<dyn Any + 'static>) {
|
2017-08-14 15:04:56 -05:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 09:49:12 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-11-05 07:29:48 -06:00
|
|
|
// The suggestion should include proper parentheses to avoid a syntax error.
|
|
|
|
pub fn test14(_display: &Box<dyn Display>) {}
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2020-11-05 07:29:48 -06:00
|
|
|
pub fn test15(_display: &Box<dyn Display + Send>) {}
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2020-11-05 07:29:48 -06:00
|
|
|
pub fn test16<'a>(_display: &'a Box<dyn Display + 'a>) {}
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2020-11-05 07:29:48 -06:00
|
|
|
|
|
|
|
pub fn test17(_display: &Box<impl Display>) {}
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2020-11-05 07:29:48 -06:00
|
|
|
pub fn test18(_display: &Box<impl Display + Send>) {}
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2020-11-05 07:29:48 -06:00
|
|
|
pub fn test19<'a>(_display: &'a Box<impl Display + 'a>) {}
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2020-11-05 07:29:48 -06:00
|
|
|
|
|
|
|
// 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)>) {}
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: you seem to be trying to use `&Box<T>`. Consider using just `&T`
|
2020-11-05 07:29:48 -06:00
|
|
|
|
2024-10-03 09:32:51 -05:00
|
|
|
#[allow(clippy::borrowed_box)]
|
|
|
|
trait Trait {
|
|
|
|
fn f(b: &Box<bool>);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trait impls are not linted
|
|
|
|
impl Trait for () {
|
|
|
|
fn f(_: &Box<bool>) {}
|
|
|
|
}
|
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
fn main() {
|
2017-06-10 22:50:57 -05:00
|
|
|
test1(&mut Box::new(false));
|
|
|
|
test2();
|
2019-05-30 01:23:47 -05:00
|
|
|
test5(&mut (Box::new(false) as Box<dyn Any>));
|
2017-08-14 15:04:56 -05:00
|
|
|
test6();
|
2019-05-30 01:23:47 -05:00
|
|
|
test9(&mut (Box::new(false) as Box<dyn Any + Send + Sync>));
|
2017-08-14 15:04:56 -05:00
|
|
|
test10();
|
2017-06-10 22:50:57 -05:00
|
|
|
}
|