about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-02-14 02:41:15 +0100
committerGitHub <noreply@github.com>2019-02-14 02:41:15 +0100
commit93ecae63d89f66af6029a002b2269e6fb3687dd4 (patch)
tree098b268f58da59c4fd385b6e8e4bb860ef36984a /src
parent4772dc8087b1d0f2bab6d064fd930e596c82d439 (diff)
parent285d4a7eb67cdf3ebba4b8fe60a5c5a6abf3edfc (diff)
downloadrust-93ecae63d89f66af6029a002b2269e6fb3687dd4.tar.gz
rust-93ecae63d89f66af6029a002b2269e6fb3687dd4.zip
Rollup merge of #57451 - dlrobertson:can_use_as_ref_nit, r=estebank
suggestion-diagnostics: as_ref improve snippet

Improve the code snippet suggested in suggestion-diagnostics when
suggesting the use of as_ref.

Given:

```rust
fn test(x: &usize) {}
fn main() {
    Some(42).map(|x| test(x));
}
```

Suggest

```
  help: consider using `as_ref` instead: `as_ref().map`
```

Instead of

```
  help: consider using `as_ref` instead: `as_ref().`
```
Diffstat (limited to 'src')
-rw-r--r--src/librustc_typeck/check/demand.rs15
-rw-r--r--src/test/ui/suggestions/as-ref.stderr16
2 files changed, 18 insertions, 13 deletions
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs
index 33e93b582e5..bb9fc872b85 100644
--- a/src/librustc_typeck/check/demand.rs
+++ b/src/librustc_typeck/check/demand.rs
@@ -210,7 +210,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
     /// ```
     /// opt.map(|arg| { takes_ref(arg) });
     /// ```
-    fn can_use_as_ref(&self, expr: &hir::Expr) -> Option<(Span, &'static str, String)> {
+    fn can_use_as_ref(
+        &self,
+        expr: &hir::Expr,
+    ) -> Option<(Span, &'static str, String)> {
         if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.node {
             if let hir::def::Def::Local(id) = path.def {
                 let parent = self.tcx.hir().get_parent_node(id);
@@ -233,10 +236,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
                             self_ty.starts_with("std::option::Option") ||
                             self_ty.starts_with("std::result::Result")
                         ) && (name == "map" || name == "and_then");
-                        if is_as_ref_able {
-                            return Some((span.shrink_to_lo(),
-                                         "consider using `as_ref` instead",
-                                         "as_ref().".into()));
+                        match (is_as_ref_able, self.sess().source_map().span_to_snippet(*span)) {
+                            (true, Ok(src)) => {
+                                return Some((*span, "consider using `as_ref` instead",
+                                             format!("as_ref().{}", src)));
+                            },
+                            _ => ()
                         }
                     }
                 }
diff --git a/src/test/ui/suggestions/as-ref.stderr b/src/test/ui/suggestions/as-ref.stderr
index 7273496a7ce..8143acc957b 100644
--- a/src/test/ui/suggestions/as-ref.stderr
+++ b/src/test/ui/suggestions/as-ref.stderr
@@ -2,9 +2,9 @@ error[E0308]: mismatched types
   --> $DIR/as-ref.rs:6:27
    |
 LL |   opt.map(|arg| takes_ref(arg));
-   |       -                   ^^^ expected &Foo, found struct `Foo`
+   |       ---                 ^^^ expected &Foo, found struct `Foo`
    |       |
-   |       help: consider using `as_ref` instead: `as_ref().`
+   |       help: consider using `as_ref` instead: `as_ref().map`
    |
    = note: expected type `&Foo`
               found type `Foo`
@@ -13,9 +13,9 @@ error[E0308]: mismatched types
   --> $DIR/as-ref.rs:8:37
    |
 LL |   opt.and_then(|arg| Some(takes_ref(arg)));
-   |       -                             ^^^ expected &Foo, found struct `Foo`
+   |       --------                      ^^^ expected &Foo, found struct `Foo`
    |       |
-   |       help: consider using `as_ref` instead: `as_ref().`
+   |       help: consider using `as_ref` instead: `as_ref().and_then`
    |
    = note: expected type `&Foo`
               found type `Foo`
@@ -24,9 +24,9 @@ error[E0308]: mismatched types
   --> $DIR/as-ref.rs:11:27
    |
 LL |   opt.map(|arg| takes_ref(arg));
-   |       -                   ^^^ expected &Foo, found struct `Foo`
+   |       ---                 ^^^ expected &Foo, found struct `Foo`
    |       |
-   |       help: consider using `as_ref` instead: `as_ref().`
+   |       help: consider using `as_ref` instead: `as_ref().map`
    |
    = note: expected type `&Foo`
               found type `Foo`
@@ -35,9 +35,9 @@ error[E0308]: mismatched types
   --> $DIR/as-ref.rs:13:35
    |
 LL |   opt.and_then(|arg| Ok(takes_ref(arg)));
-   |       -                           ^^^ expected &Foo, found struct `Foo`
+   |       --------                    ^^^ expected &Foo, found struct `Foo`
    |       |
-   |       help: consider using `as_ref` instead: `as_ref().`
+   |       help: consider using `as_ref` instead: `as_ref().and_then`
    |
    = note: expected type `&Foo`
               found type `Foo`