@@ -9,7 +9,7 @@ use rustc_hir as hir;
99use rustc_hir:: HirIdSet ;
1010use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
1111use rustc_middle:: lint:: in_external_macro;
12- use rustc_middle:: ty:: { Ty , TyS } ;
12+ use rustc_middle:: ty:: TyS ;
1313use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
1414use rustc_span:: sym;
1515
@@ -65,6 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
6565 if let hir:: ItemKind :: Impl ( hir:: Impl {
6666 of_trait : None ,
6767 ref generics,
68+ self_ty : impl_self_ty,
6869 items,
6970 ..
7071 } ) = item. kind
@@ -132,21 +133,23 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
132133 }
133134
134135 let generics_sugg = snippet( cx, generics. span, "" ) ;
136+ let self_ty_fmt = self_ty. to_string( ) ;
137+ let self_type_snip = snippet( cx, impl_self_ty. span, & self_ty_fmt) ;
135138 span_lint_hir_and_then(
136139 cx,
137140 NEW_WITHOUT_DEFAULT ,
138141 id,
139142 impl_item. span,
140143 & format!(
141144 "you should consider adding a `Default` implementation for `{}`" ,
142- self_ty
145+ self_type_snip
143146 ) ,
144147 |diag| {
145148 diag. suggest_prepend_item(
146149 cx,
147150 item. span,
148- "try this" ,
149- & create_new_without_default_suggest_msg( self_ty , & generics_sugg) ,
151+ "try adding this" ,
152+ & create_new_without_default_suggest_msg( & self_type_snip , & generics_sugg) ,
150153 Applicability :: MaybeIncorrect ,
151154 ) ;
152155 } ,
@@ -160,12 +163,12 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
160163 }
161164}
162165
163- fn create_new_without_default_suggest_msg ( ty : Ty < ' _ > , generics_sugg : & str ) -> String {
166+ fn create_new_without_default_suggest_msg ( self_type_snip : & str , generics_sugg : & str ) -> String {
164167 #[ rustfmt:: skip]
165168 format ! (
166169"impl{} Default for {} {{
167170 fn default() -> Self {{
168171 Self::new()
169172 }}
170- }}" , generics_sugg, ty )
173+ }}" , generics_sugg, self_type_snip )
171174}
0 commit comments