@@ -914,7 +914,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
914
914
} ) ;
915
915
916
916
if result. is_ok ( ) {
917
- self . maybe_lint_bare_trait ( qpath, hir_id) ;
917
+ self . maybe_lint_bare_trait ( qpath, hir_id, span ) ;
918
918
self . register_wf_obligation ( ty. into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
919
919
}
920
920
@@ -927,18 +927,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
927
927
)
928
928
}
929
929
930
- fn maybe_lint_bare_trait ( & self , qpath : & QPath < ' _ > , hir_id : hir:: HirId ) {
930
+ fn maybe_lint_bare_trait ( & self , qpath : & QPath < ' _ > , hir_id : hir:: HirId , span : Span ) {
931
931
if let QPath :: TypeRelative ( self_ty, _) = qpath {
932
932
if let TyKind :: TraitObject ( [ poly_trait_ref, ..] , _, TraitObjectSyntax :: None ) =
933
933
self_ty. kind
934
934
{
935
935
let msg = "trait objects without an explicit `dyn` are deprecated" ;
936
936
let ( sugg, app) = match self . tcx . sess . source_map ( ) . span_to_snippet ( self_ty. span ) {
937
937
Ok ( s) if poly_trait_ref. trait_ref . path . is_global ( ) => {
938
- ( format ! ( "< dyn ({})> " , s) , Applicability :: MachineApplicable )
938
+ ( format ! ( "dyn ({})" , s) , Applicability :: MachineApplicable )
939
939
}
940
- Ok ( s) => ( format ! ( "<dyn {}>" , s) , Applicability :: MachineApplicable ) ,
941
- Err ( _) => ( "<dyn <type>>" . to_string ( ) , Applicability :: HasPlaceholders ) ,
940
+ Ok ( s) => ( format ! ( "dyn {}" , s) , Applicability :: MachineApplicable ) ,
941
+ Err ( _) => ( "dyn <type>" . to_string ( ) , Applicability :: HasPlaceholders ) ,
942
+ } ;
943
+ // Wrap in `<..>` if it isn't already.
944
+ let sugg = match self . tcx . sess . source_map ( ) . span_to_snippet ( span) {
945
+ Ok ( s) if s. starts_with ( '<' ) => sugg,
946
+ _ => format ! ( "<{}>" , sugg) ,
942
947
} ;
943
948
let replace = String :: from ( "use `dyn`" ) ;
944
949
if self . sess ( ) . edition ( ) >= Edition :: Edition2021 {
0 commit comments