fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![warn(clippy::box_default)] 2#![allow(clippy::default_constructed_unit_structs)] 3 4#[derive(Default)] 5struct ImplementsDefault; 6 7struct OwnDefault; 8 9impl OwnDefault { 10 fn default() -> Self { 11 Self 12 } 13} 14 15macro_rules! outer { 16 ($e: expr) => { 17 $e 18 }; 19} 20 21fn main() { 22 let _string: Box<String> = Box::default(); 23 let _byte = Box::<u8>::default(); 24 let _vec = Box::<Vec<u8>>::default(); 25 let _impl = Box::<ImplementsDefault>::default(); 26 let _impl2 = Box::<ImplementsDefault>::default(); 27 let _impl3: Box<ImplementsDefault> = Box::default(); 28 let _own = Box::new(OwnDefault::default()); // should not lint 29 let _in_macro = outer!(Box::<String>::default()); 30 let _string_default = outer!(Box::<String>::default()); 31 let _vec2: Box<Vec<ImplementsDefault>> = Box::default(); 32 let _vec3: Box<Vec<bool>> = Box::default(); 33 let _vec4: Box<_> = Box::<Vec<bool>>::default(); 34 let _more = ret_ty_fn(); 35 call_ty_fn(Box::default()); 36 issue_10381(); 37 38 // `Box::<Option<_>>::default()` would be valid here, but not `Box::default()` or 39 // `Box::<Option<{closure@...}>::default()` 40 // 41 // Would have a suggestion after https://github.com/rust-lang/rust/blob/fdd030127cc68afec44a8d3f6341525dd34e50ae/compiler/rustc_middle/src/ty/diagnostics.rs#L554-L563 42 let mut unnameable = Box::new(Option::default()); 43 let _ = unnameable.insert(|| {}); 44} 45 46fn ret_ty_fn() -> Box<bool> { 47 Box::<bool>::default() 48} 49 50#[allow(clippy::boxed_local)] 51fn call_ty_fn(_b: Box<u8>) { 52 issue_9621_dyn_trait(); 53} 54 55use std::io::{Read, Result}; 56 57impl Read for ImplementsDefault { 58 fn read(&mut self, _: &mut [u8]) -> Result<usize> { 59 Ok(0) 60 } 61} 62 63fn issue_9621_dyn_trait() { 64 let _: Box<dyn Read> = Box::<ImplementsDefault>::default(); 65 issue_10089(); 66} 67 68fn issue_10089() { 69 let _closure = || { 70 #[derive(Default)] 71 struct WeirdPathed; 72 73 let _ = Box::<WeirdPathed>::default(); 74 }; 75} 76 77fn issue_10381() { 78 #[derive(Default)] 79 pub struct Foo {} 80 pub trait Bar {} 81 impl Bar for Foo {} 82 83 fn maybe_get_bar(i: u32) -> Option<Box<dyn Bar>> { 84 if i % 2 == 0 { 85 Some(Box::<Foo>::default()) 86 } else { 87 None 88 } 89 } 90 91 assert!(maybe_get_bar(2).is_some()); 92}