about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc_typeck/check/method/suggest.rs25
-rw-r--r--src/test/ui/suggestions/remove-as_str.stderr8
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 b4b84b61fd6..70cb799cc6e 100644
--- a/src/librustc_typeck/check/method/suggest.rs
+++ b/src/librustc_typeck/check/method/suggest.rs
@@ -538,13 +538,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 2e8b72ebd4f..eae9cc07508 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