Skip to content

Commit edbbfad

Browse files
committed
Suggest removal of & when borrowing macro and appropriate
Fix #58815.
1 parent c1d2d83 commit edbbfad

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

src/librustc_typeck/check/demand.rs

+9
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
367367
// Maybe remove `&`?
368368
hir::ExprKind::AddrOf(_, ref expr) => {
369369
if !cm.span_to_filename(expr.span).is_real() {
370+
if let Ok(code) = cm.span_to_snippet(sp) {
371+
if code.chars().next() == Some('&') {
372+
return Some((
373+
sp,
374+
"consider removing the borrow",
375+
code[1..].to_string()),
376+
);
377+
}
378+
}
370379
return None;
371380
}
372381
if let Ok(code) = cm.span_to_snippet(expr.span) {

src/test/ui/block-result/issue-5500.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0308]: mismatched types
44
LL | fn main() {
55
| - expected `()` because of default return type
66
LL | &panic!()
7-
| ^^^^^^^^^ expected (), found reference
7+
| ^^^^^^^^^
8+
| |
9+
| expected (), found reference
10+
| help: consider removing the borrow: `panic!()`
811
|
912
= note: expected type `()`
1013
found type `&_`

src/test/ui/diverging-tuple-parts-39485.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
error[E0308]: mismatched types
22
--> $DIR/diverging-tuple-parts-39485.rs:8:5
33
|
4-
LL | fn g() {
5-
| - help: try adding a return type: `-> &_`
64
LL | &panic!() //~ ERROR mismatched types
75
| ^^^^^^^^^ expected (), found reference
86
|
97
= note: expected type `()`
108
found type `&_`
9+
help: try adding a return type
10+
|
11+
LL | fn g() -> &_ {
12+
| ^^^^^
13+
help: consider removing the borrow
14+
|
15+
LL | panic!() //~ ERROR mismatched types
16+
| ^^^^^^^^
1117

1218
error[E0308]: mismatched types
1319
--> $DIR/diverging-tuple-parts-39485.rs:12:5
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let a: String = &String::from("a");
3+
//~^ ERROR mismatched types
4+
let b: String = &format!("b");
5+
//~^ ERROR mismatched types
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/format-borrow.rs:2:21
3+
|
4+
LL | let a: String = &String::from("a");
5+
| ^^^^^^^^^^^^^^^^^^
6+
| |
7+
| expected struct `std::string::String`, found reference
8+
| help: consider removing the borrow: `String::from("a")`
9+
|
10+
= note: expected type `std::string::String`
11+
found type `&std::string::String`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/format-borrow.rs:4:21
15+
|
16+
LL | let b: String = &format!("b");
17+
| ^^^^^^^^^^^^^
18+
| |
19+
| expected struct `std::string::String`, found reference
20+
| help: consider removing the borrow: `format!("b")`
21+
|
22+
= note: expected type `std::string::String`
23+
found type `&std::string::String`
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)