about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_hir_typeck/src/expr.rs17
-rw-r--r--tests/ui/privacy/suggest-box-new.stderr8
2 files changed, 25 insertions, 0 deletions
diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs
index 9f27a6e5d84..4c177dbab79 100644
--- a/compiler/rustc_hir_typeck/src/expr.rs
+++ b/compiler/rustc_hir_typeck/src/expr.rs
@@ -2165,6 +2165,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     );
                 }
             }
+            if let Some(default_trait) = self.tcx.get_diagnostic_item(sym::Default)
+                && self.infcx
+                    .type_implements_trait(default_trait, [adt_ty], self.param_env)
+                    .may_apply()
+            {
+                err.multipart_suggestion(
+                    "consider using the `Default` trait",
+                    vec![
+                        (span.shrink_to_lo(), "<".to_string()),
+                        (
+                            span.shrink_to_hi().with_hi(expr_span.hi()),
+                            " as std::default::Default>::default()".to_string(),
+                        ),
+                    ],
+                    Applicability::MaybeIncorrect,
+                );
+            }
         }
 
         err.emit();
diff --git a/tests/ui/privacy/suggest-box-new.stderr b/tests/ui/privacy/suggest-box-new.stderr
index 79629dab6be..5fec35dc5c6 100644
--- a/tests/ui/privacy/suggest-box-new.stderr
+++ b/tests/ui/privacy/suggest-box-new.stderr
@@ -67,6 +67,10 @@ LL |     let _ = std::collections::HashMap::with_hasher(_);
    |                                      ~~~~~~~~~~~~~~~~
 LL |     let _ = std::collections::HashMap::with_capacity_and_hasher(_, _);
    |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+help: consider using the `Default` trait
+   |
+LL |     let _ = <std::collections::HashMap as std::default::Default>::default();
+   |             +                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 error: cannot construct `Box<_, _>` with struct literal syntax due to private fields
   --> $DIR/suggest-box-new.rs:18:13
@@ -86,6 +90,10 @@ LL |     let _ = Box::new_zeroed();
 LL |     let _ = Box::new_in(_, _);
    |                ~~~~~~~~~~~~~~
      and 10 other candidates
+help: consider using the `Default` trait
+   |
+LL |     let _ = <Box as std::default::Default>::default();
+   |             +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 error: aborting due to 4 previous errors