@@ -35,14 +35,15 @@ impl<'tcx> LateLintPass<'tcx> for TrivialDefaultConstructedTypes {
35
35
&& let ExprKind :: Call ( call, _) = expr. kind
36
36
&& is_trait_item ( cx, call, sym:: Default )
37
37
{
38
+ let mut app = Applicability :: MachineApplicable ;
38
39
let ret_ty = cx
39
40
. typeck_results ( )
40
41
. expr_ty ( call)
41
42
. fn_sig ( cx. tcx )
42
43
. output ( )
43
44
. skip_binder ( )
44
45
. peel_refs ( ) ;
45
- if let Some ( default) = default_value ( cx, ret_ty) && !is_from_proc_macro ( cx, expr) {
46
+ if let Some ( default) = default_value ( cx, ret_ty, & mut app ) && !is_from_proc_macro ( cx, expr) {
46
47
span_lint_and_sugg (
47
48
cx,
48
49
TRIVIAL_DEFAULT_CONSTRUCTED_TYPES ,
@@ -55,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialDefaultConstructedTypes {
55
56
} else if let ty:: Tuple ( fields) = ret_ty. kind ( )
56
57
&& fields. len ( ) <= 3
57
58
&& let Some ( fields_default) = fields. iter ( )
58
- . map ( |field| default_value ( cx, field) )
59
+ . map ( |field| default_value ( cx, field, & mut app ) )
59
60
. collect :: < Option < Vec < _ > > > ( )
60
61
&& !is_from_proc_macro ( cx, expr)
61
62
{
@@ -79,7 +80,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialDefaultConstructedTypes {
79
80
Applicability :: MachineApplicable ,
80
81
) ;
81
82
} else if let ty:: Array ( ty, len) = ret_ty. kind ( )
82
- && let Some ( default) = default_value ( cx, * ty)
83
+ && let Some ( default) = default_value ( cx, * ty, & mut app )
83
84
&& !is_from_proc_macro ( cx, expr)
84
85
{
85
86
span_lint_and_sugg (
@@ -89,18 +90,21 @@ impl<'tcx> LateLintPass<'tcx> for TrivialDefaultConstructedTypes {
89
90
"constructing a trivial array using `default`" ,
90
91
"try" ,
91
92
format ! ( "[{default}; {len}]" ) ,
92
- Applicability :: MachineApplicable ,
93
+ app ,
93
94
) ;
94
95
}
95
96
}
96
97
}
97
98
}
98
99
99
100
/// Gets the default value of `ty`.
100
- fn default_value ( cx : & LateContext < ' _ > , ty : Ty < ' _ > ) -> Option < Cow < ' static , str > > {
101
+ fn default_value ( cx : & LateContext < ' _ > , ty : Ty < ' _ > , app : & mut Applicability ) -> Option < Cow < ' static , str > > {
101
102
match ty. kind ( ) {
102
- ty:: Adt ( def, substs) if let [ subst] = substs. as_slice ( ) => {
103
- is_lang_item_or_ctor ( cx, def. did ( ) , LangItem :: Option ) . then ( || format ! ( "None::<{subst}>" ) . into ( ) )
103
+ ty:: Adt ( def, _) => {
104
+ * app = Applicability :: HasPlaceholders ;
105
+ // Checking if the generic argument is required would substantially increase the
106
+ // complexity of this lint, for now, just use a placeholder (`_`).
107
+ is_lang_item_or_ctor ( cx, def. did ( ) , LangItem :: Option ) . then ( || "None::<_>" . into ( ) )
104
108
} ,
105
109
ty:: Bool => Some ( "false" . into ( ) ) ,
106
110
ty:: Str => Some ( r#""""# . into ( ) ) ,
0 commit comments