Skip to content

Commit 23d2cb8

Browse files
committed
Don't suggest extra <> in dyn suggestion.
1 parent 13edc17 commit 23d2cb8

File tree

1 file changed

+10
-5
lines changed
  • compiler/rustc_typeck/src/check/fn_ctxt

1 file changed

+10
-5
lines changed

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
914914
});
915915

916916
if result.is_ok() {
917-
self.maybe_lint_bare_trait(qpath, hir_id);
917+
self.maybe_lint_bare_trait(qpath, hir_id, span);
918918
self.register_wf_obligation(ty.into(), qself.span, traits::WellFormed(None));
919919
}
920920

@@ -927,18 +927,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
927927
)
928928
}
929929

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) {
931931
if let QPath::TypeRelative(self_ty, _) = qpath {
932932
if let TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
933933
self_ty.kind
934934
{
935935
let msg = "trait objects without an explicit `dyn` are deprecated";
936936
let (sugg, app) = match self.tcx.sess.source_map().span_to_snippet(self_ty.span) {
937937
Ok(s) if poly_trait_ref.trait_ref.path.is_global() => {
938-
(format!("<dyn ({})>", s), Applicability::MachineApplicable)
938+
(format!("dyn ({})", s), Applicability::MachineApplicable)
939939
}
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),
942947
};
943948
let replace = String::from("use `dyn`");
944949
if self.sess().edition() >= Edition::Edition2021 {

0 commit comments

Comments
 (0)