2022-10-23 08:18:45 -05:00
#![ warn(clippy::box_default) ]
2023-05-05 10:45:49 -05:00
#![ allow(clippy::default_constructed_unit_structs) ]
2022-10-23 08:18:45 -05:00
#[ 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 ::default ( ) ;
let _byte = Box ::< u8 > ::default ( ) ;
2024-02-26 05:58:41 -06:00
let _vec = Box ::< Vec ::< u8 > > ::default ( ) ;
2022-10-23 08:18:45 -05:00
let _impl = Box ::< ImplementsDefault > ::default ( ) ;
let _impl2 = Box ::< ImplementsDefault > ::default ( ) ;
let _impl3 : Box < ImplementsDefault > = Box ::default ( ) ;
let _own = Box ::new ( OwnDefault ::default ( ) ) ; // should not lint
2023-01-12 12:48:13 -06:00
let _in_macro = outer! ( Box ::< String > ::default ( ) ) ;
let _string_default = outer! ( Box ::< String > ::default ( ) ) ;
2022-10-23 08:18:45 -05:00
let _vec2 : Box < Vec < ImplementsDefault > > = Box ::default ( ) ;
let _vec3 : Box < Vec < bool > > = Box ::default ( ) ;
2023-01-12 12:48:13 -06:00
let _vec4 : Box < _ > = Box ::< Vec < bool > > ::default ( ) ;
2022-10-23 08:18:45 -05:00
let _more = ret_ty_fn ( ) ;
call_ty_fn ( Box ::default ( ) ) ;
2023-02-25 18:08:29 -06:00
issue_10381 ( ) ;
2023-05-20 08:39:26 -05:00
// `Box::<Option<_>>::default()` would be valid here, but not `Box::default()` or
2023-09-09 01:36:50 -05:00
// `Box::<Option<{closure@...}>::default()`
2023-05-20 08:39:26 -05:00
//
// Would have a suggestion after https://github.com/rust-lang/rust/blob/fdd030127cc68afec44a8d3f6341525dd34e50ae/compiler/rustc_middle/src/ty/diagnostics.rs#L554-L563
let mut unnameable = Box ::new ( Option ::default ( ) ) ;
let _ = unnameable . insert ( | | { } ) ;
2022-10-23 08:18:45 -05:00
}
fn ret_ty_fn ( ) -> Box < bool > {
Box ::< bool > ::default ( )
}
#[ allow(clippy::boxed_local) ]
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 ::< ImplementsDefault > ::default ( ) ;
2023-01-12 12:48:13 -06:00
issue_10089 ( ) ;
}
fn issue_10089 ( ) {
let _closure = | | {
#[ derive(Default) ]
struct WeirdPathed ;
let _ = Box ::< WeirdPathed > ::default ( ) ;
} ;
2022-10-23 08:18:45 -05:00
}
2023-02-25 18:08:29 -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 ::< Foo > ::default ( ) )
} else {
None
}
}
assert! ( maybe_get_bar ( 2 ) . is_some ( ) ) ;
}
2023-11-26 11:11:50 -06:00
#[ allow(unused) ]
fn issue_11868 ( ) {
fn foo ( _ : & mut Vec < usize > ) { }
macro_rules ! bar {
( $baz :expr ) = > {
Box ::leak ( Box ::new ( $baz ) )
} ;
}
foo ( bar! ( vec! [ ] ) ) ;
foo ( bar! ( vec! [ 1 ] ) ) ;
}
2024-02-26 05:58:41 -06:00
// Issue #11927: The quickfix for the `Box::new` suggests replacing with `Box::<Inner>::default()`,
// removing the `outer::` segment.
fn issue_11927 ( ) {
mod outer {
#[ derive(Default) ]
pub struct Inner {
_i : usize ,
}
}
fn foo ( ) {
let _b = Box ::< outer ::Inner > ::default ( ) ;
let _b = Box ::< std ::collections ::HashSet ::< i32 > > ::default ( ) ;
}
}