From 5aa37a9c9b99b62a4e8827c133b6a8f97e3b5734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 7 Oct 2019 16:18:02 -0700 Subject: [PATCH] Use structured suggestion for removal of `as_str()` call --- src/librustc_typeck/check/method/suggest.rs | 25 +++++++++++++++----- src/test/ui/suggestions/remove-as_str.stderr | 8 +++---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 9820ede5b5c4e..e058aeddfb8fe 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -518,13 +518,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } + let mut fallback_span = true; + let msg = "remove this method call"; if item_name.as_str() == "as_str" && actual.peel_refs().is_str() { - // FIXME: the span is not quite correct, it should point to ".as_str()" instead - // of just "as_str". - err.span_label( - span, - "try removing `as_str`" - ); + if let SelfSource::MethodCall(expr) = source { + let call_expr = self.tcx.hir().expect_expr( + self.tcx.hir().get_parent_node(expr.hir_id), + ); + if let Some(span) = call_expr.span.trim_start(expr.span) { + err.span_suggestion( + span, + msg, + String::new(), + Applicability::MachineApplicable, + ); + fallback_span = false; + } + } + if fallback_span { + err.span_label(span, msg); + } } else if let Some(lev_candidate) = lev_candidate { let def_kind = lev_candidate.def_kind(); err.span_suggestion( diff --git a/src/test/ui/suggestions/remove-as_str.stderr b/src/test/ui/suggestions/remove-as_str.stderr index 2e8b72ebd4f6d..eae9cc075084a 100644 --- a/src/test/ui/suggestions/remove-as_str.stderr +++ b/src/test/ui/suggestions/remove-as_str.stderr @@ -2,25 +2,25 @@ error[E0599]: no method named `as_str` found for type `&str` in the current scop --> $DIR/remove-as_str.rs:2:7 | LL | s.as_str(); - | ^^^^^^ try removing `as_str` + | -^^^^^^-- help: remove this method call error[E0599]: no method named `as_str` found for type `&'a str` in the current scope --> $DIR/remove-as_str.rs:7:7 | LL | s.as_str(); - | ^^^^^^ try removing `as_str` + | -^^^^^^-- help: remove this method call error[E0599]: no method named `as_str` found for type `&mut str` in the current scope --> $DIR/remove-as_str.rs:12:7 | LL | s.as_str(); - | ^^^^^^ try removing `as_str` + | -^^^^^^-- help: remove this method call error[E0599]: no method named `as_str` found for type `&&str` in the current scope --> $DIR/remove-as_str.rs:17:7 | LL | s.as_str(); - | ^^^^^^ try removing `as_str` + | -^^^^^^-- help: remove this method call error: aborting due to 4 previous errors