2022-10-23 08:18:45 -05:00
#![ warn(clippy::box_default) ]
2024-03-31 08:22:04 -05:00
#![ allow(clippy::boxed_local, 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
}
}
2024-03-31 08:22:04 -05:00
macro_rules ! default {
( ) = > {
Default ::default ( )
} ;
}
macro_rules ! string_new {
( ) = > {
String ::new ( )
} ;
}
macro_rules ! box_new {
( $e :expr ) = > {
Box ::new ( $e )
2022-10-23 08:18:45 -05:00
} ;
}
fn main ( ) {
2024-03-31 08:22:04 -05:00
let string1 : Box < String > = Box ::default ( ) ;
let string2 : Box < String > = Box ::default ( ) ;
let impl1 : Box < ImplementsDefault > = Box ::default ( ) ;
let vec : Box < Vec < u8 > > = Box ::default ( ) ;
let byte : Box < u8 > = Box ::default ( ) ;
let vec2 : Box < Vec < ImplementsDefault > > = Box ::default ( ) ;
let vec3 : Box < Vec < bool > > = Box ::default ( ) ;
let plain_default = Box ::default ( ) ;
let _ : Box < String > = plain_default ;
let _ : Box < String > = Box ::new ( default! ( ) ) ;
let _ : Box < String > = Box ::new ( string_new! ( ) ) ;
let _ : Box < String > = box_new! ( Default ::default ( ) ) ;
let _ : Box < String > = box_new! ( String ::new ( ) ) ;
let _ : Box < String > = box_new! ( default! ( ) ) ;
let _ : Box < String > = box_new! ( string_new! ( ) ) ;
let own : Box < OwnDefault > = Box ::new ( OwnDefault ::default ( ) ) ; // should not lint
// Do not suggest where a turbofish would be required
let impl2 = Box ::new ( ImplementsDefault ::default ( ) ) ;
let impl3 = Box ::new ( < ImplementsDefault as Default > ::default ( ) ) ;
let vec4 : Box < _ > = Box ::new ( Vec ::from ( [ false ; 0 ] ) ) ;
let more = ret_ty_fn ( ) ;
2022-10-23 08:18:45 -05:00
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 ( | | { } ) ;
2024-04-17 09:40:00 -05:00
let _ = Box ::into_raw ( Box ::new ( String ::default ( ) ) ) ;
2022-10-23 08:18:45 -05:00
}
fn ret_ty_fn ( ) -> Box < bool > {
2024-03-31 08:22:04 -05:00
Box ::new ( bool ::default ( ) ) // Could lint, currently doesn't
2022-10-23 08:18:45 -05:00
}
fn call_ty_fn ( _b : Box < u8 > ) {
issue_9621_dyn_trait ( ) ;
}
2024-04-17 09:40:00 -05:00
struct X < T > ( T ) ;
impl < T : Default > X < T > {
fn x ( _ : Box < T > ) { }
fn same_generic_param ( ) {
Self ::x ( Box ::default ( ) ) ;
}
}
2022-10-23 08:18:45 -05:00
use std ::io ::{ Read , Result } ;
impl Read for ImplementsDefault {
fn read ( & mut self , _ : & mut [ u8 ] ) -> Result < usize > {
Ok ( 0 )
}
}
fn issue_9621_dyn_trait ( ) {
2024-03-31 08:22:04 -05:00
let _ : Box < dyn Read > = Box ::new ( ImplementsDefault ::default ( ) ) ;
2023-01-12 12:48:13 -06:00
issue_10089 ( ) ;
}
fn issue_10089 ( ) {
let _closure = | | {
#[ derive(Default) ]
struct WeirdPathed ;
2024-03-31 08:22:04 -05:00
let _ = Box ::new ( WeirdPathed ::default ( ) ) ;
2023-01-12 12:48:13 -06:00
} ;
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 {
2024-03-31 08:22:04 -05:00
Some ( Box ::new ( Foo ::default ( ) ) )
2023-02-25 18:08:29 -06:00
} else {
None
}
}
assert! ( maybe_get_bar ( 2 ) . is_some ( ) ) ;
}
2023-11-26 11:11:50 -06:00
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 ( ) {
2024-03-31 08:22:04 -05:00
let _b = Box ::new ( outer ::Inner ::default ( ) ) ;
let _b = Box ::new ( std ::collections ::HashSet ::< i32 > ::new ( ) ) ;
2024-02-26 05:58:41 -06:00
}
}