@@ -242,6 +242,20 @@ impl<'tcx> LateLintPass<'tcx> for DefaultCouldBeDerived {
242242 {
243243 let type_def_id = cx. tcx . parent ( ctor_def_id) ; // From Ctor to struct
244244 if args. iter ( ) . all ( |expr| check_expr ( cx. tcx , expr. kind ) ) {
245+ // We have a struct literal
246+ //
247+ // struct Foo(Type);
248+ //
249+ // impl Default for Foo {
250+ // fn default() -> Foo {
251+ // Foo(val)
252+ // }
253+ // }
254+ //
255+ // We suggest #[derive(Default)] if
256+ // - `val` is `Default::default()`
257+ // - `val` is `0`
258+ // - `val` is `false`
245259 cx. tcx . node_span_lint (
246260 DEFAULT_COULD_BE_DERIVED ,
247261 item. hir_id ( ) ,
@@ -264,6 +278,41 @@ impl<'tcx> LateLintPass<'tcx> for DefaultCouldBeDerived {
264278 }
265279 }
266280 }
281+ hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( _, path) )
282+ if let Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , CtorKind :: Const ) , _) =
283+ path. res =>
284+ {
285+ // We have a struct literal
286+ //
287+ // struct Foo;
288+ //
289+ // impl Default for Foo {
290+ // fn default() -> Foo {
291+ // Foo
292+ // }
293+ // }
294+ //
295+ // We always suggest `#[derive(Default)]`.
296+ cx. tcx . node_span_lint (
297+ DEFAULT_COULD_BE_DERIVED ,
298+ item. hir_id ( ) ,
299+ item. span ,
300+ |diag| {
301+ diag. primary_message ( "`impl Default` that could be derived" ) ;
302+ diag. multipart_suggestion_verbose (
303+ "you don't need to manually `impl Default`, you can derive it" ,
304+ vec ! [
305+ (
306+ cx. tcx. def_span( type_def_id) . shrink_to_lo( ) ,
307+ "#[derive(Default)] " . to_string( ) ,
308+ ) ,
309+ ( item. span, String :: new( ) ) ,
310+ ] ,
311+ Applicability :: MachineApplicable ,
312+ ) ;
313+ } ,
314+ ) ;
315+ }
267316 _ => { }
268317 }
269318 }
0 commit comments