Skip to content

Commit 93ecae6

Browse files
authored
Rollup merge of rust-lang#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().` ```
2 parents 4772dc8 + 285d4a7 commit 93ecae6

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/librustc_typeck/check/demand.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
210210
/// ```
211211
/// opt.map(|arg| { takes_ref(arg) });
212212
/// ```
213-
fn can_use_as_ref(&self, expr: &hir::Expr) -> Option<(Span, &'static str, String)> {
213+
fn can_use_as_ref(
214+
&self,
215+
expr: &hir::Expr,
216+
) -> Option<(Span, &'static str, String)> {
214217
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.node {
215218
if let hir::def::Def::Local(id) = path.def {
216219
let parent = self.tcx.hir().get_parent_node(id);
@@ -233,10 +236,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
233236
self_ty.starts_with("std::option::Option") ||
234237
self_ty.starts_with("std::result::Result")
235238
) && (name == "map" || name == "and_then");
236-
if is_as_ref_able {
237-
return Some((span.shrink_to_lo(),
238-
"consider using `as_ref` instead",
239-
"as_ref().".into()));
239+
match (is_as_ref_able, self.sess().source_map().span_to_snippet(*span)) {
240+
(true, Ok(src)) => {
241+
return Some((*span, "consider using `as_ref` instead",
242+
format!("as_ref().{}", src)));
243+
},
244+
_ => ()
240245
}
241246
}
242247
}

src/test/ui/suggestions/as-ref.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0308]: mismatched types
22
--> $DIR/as-ref.rs:6:27
33
|
44
LL | opt.map(|arg| takes_ref(arg));
5-
| - ^^^ expected &Foo, found struct `Foo`
5+
| --- ^^^ expected &Foo, found struct `Foo`
66
| |
7-
| help: consider using `as_ref` instead: `as_ref().`
7+
| help: consider using `as_ref` instead: `as_ref().map`
88
|
99
= note: expected type `&Foo`
1010
found type `Foo`
@@ -13,9 +13,9 @@ error[E0308]: mismatched types
1313
--> $DIR/as-ref.rs:8:37
1414
|
1515
LL | opt.and_then(|arg| Some(takes_ref(arg)));
16-
| - ^^^ expected &Foo, found struct `Foo`
16+
| -------- ^^^ expected &Foo, found struct `Foo`
1717
| |
18-
| help: consider using `as_ref` instead: `as_ref().`
18+
| help: consider using `as_ref` instead: `as_ref().and_then`
1919
|
2020
= note: expected type `&Foo`
2121
found type `Foo`
@@ -24,9 +24,9 @@ error[E0308]: mismatched types
2424
--> $DIR/as-ref.rs:11:27
2525
|
2626
LL | opt.map(|arg| takes_ref(arg));
27-
| - ^^^ expected &Foo, found struct `Foo`
27+
| --- ^^^ expected &Foo, found struct `Foo`
2828
| |
29-
| help: consider using `as_ref` instead: `as_ref().`
29+
| help: consider using `as_ref` instead: `as_ref().map`
3030
|
3131
= note: expected type `&Foo`
3232
found type `Foo`
@@ -35,9 +35,9 @@ error[E0308]: mismatched types
3535
--> $DIR/as-ref.rs:13:35
3636
|
3737
LL | opt.and_then(|arg| Ok(takes_ref(arg)));
38-
| - ^^^ expected &Foo, found struct `Foo`
38+
| -------- ^^^ expected &Foo, found struct `Foo`
3939
| |
40-
| help: consider using `as_ref` instead: `as_ref().`
40+
| help: consider using `as_ref` instead: `as_ref().and_then`
4141
|
4242
= note: expected type `&Foo`
4343
found type `Foo`

0 commit comments

Comments
 (0)