2022-10-03 10:29:38 -05:00
|
|
|
// run-rustfix
|
2022-09-21 16:43:49 -05:00
|
|
|
#![warn(clippy::box_default)]
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct ImplementsDefault;
|
|
|
|
|
|
|
|
struct OwnDefault;
|
|
|
|
|
|
|
|
impl OwnDefault {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! outer {
|
|
|
|
($e: expr) => {
|
|
|
|
$e
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _string: Box<String> = Box::new(Default::default());
|
|
|
|
let _byte = Box::new(u8::default());
|
|
|
|
let _vec = Box::new(Vec::<u8>::new());
|
|
|
|
let _impl = Box::new(ImplementsDefault::default());
|
|
|
|
let _impl2 = Box::new(<ImplementsDefault as Default>::default());
|
|
|
|
let _impl3: Box<ImplementsDefault> = Box::new(Default::default());
|
|
|
|
let _own = Box::new(OwnDefault::default()); // should not lint
|
|
|
|
let _in_macro = outer!(Box::new(String::new()));
|
2022-10-03 10:29:38 -05:00
|
|
|
let _string_default = outer!(Box::new(String::from("")));
|
2022-09-21 16:43:49 -05:00
|
|
|
let _vec2: Box<Vec<ImplementsDefault>> = Box::new(vec![]);
|
2022-10-03 10:29:38 -05:00
|
|
|
let _vec3: Box<Vec<bool>> = Box::new(Vec::from([]));
|
|
|
|
let _vec4: Box<_> = Box::new(Vec::from([false; 0]));
|
|
|
|
let _more = ret_ty_fn();
|
|
|
|
call_ty_fn(Box::new(u8::default()));
|
2023-02-19 12:25:53 -06:00
|
|
|
issue_10381();
|
2022-09-21 16:43:49 -05:00
|
|
|
}
|
2022-10-03 10:29:38 -05:00
|
|
|
|
|
|
|
fn ret_ty_fn() -> Box<bool> {
|
|
|
|
Box::new(bool::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::boxed_local)]
|
2022-10-10 06:05:07 -05:00
|
|
|
fn call_ty_fn(_b: Box<u8>) {
|
|
|
|
issue_9621_dyn_trait();
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::io::{Read, Result};
|
|
|
|
|
|
|
|
impl Read for ImplementsDefault {
|
|
|
|
fn read(&mut self, _: &mut [u8]) -> Result<usize> {
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn issue_9621_dyn_trait() {
|
|
|
|
let _: Box<dyn Read> = Box::new(ImplementsDefault::default());
|
2023-01-08 13:25:42 -06:00
|
|
|
issue_10089();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn issue_10089() {
|
|
|
|
let _closure = || {
|
|
|
|
#[derive(Default)]
|
|
|
|
struct WeirdPathed;
|
|
|
|
|
|
|
|
let _ = Box::new(WeirdPathed::default());
|
|
|
|
};
|
2022-10-10 06:05:07 -05:00
|
|
|
}
|
2023-02-19 12:25:53 -06:00
|
|
|
|
|
|
|
fn issue_10381() {
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Foo {}
|
|
|
|
pub trait Bar {}
|
|
|
|
impl Bar for Foo {}
|
|
|
|
|
|
|
|
fn maybe_get_bar(i: u32) -> Option<Box<dyn Bar>> {
|
|
|
|
if i % 2 == 0 {
|
|
|
|
Some(Box::new(Foo::default()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(maybe_get_bar(2).is_some());
|
|
|
|
}
|