mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 00:34:06 +00:00
Suggest Default::default() for struct literals with private fields
This commit is contained in:
parent
be0958f5ab
commit
69edf8e784
@ -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();
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user