Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest removal of & when borrowing macro and appropriate #58877

Merged
merged 1 commit into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// Maybe remove `&`?
hir::ExprKind::AddrOf(_, ref expr) => {
if !cm.span_to_filename(expr.span).is_real() {
if let Ok(code) = cm.span_to_snippet(sp) {
if code.chars().next() == Some('&') {
return Some((
sp,
"consider removing the borrow",
code[1..].to_string()),
);
}
}
return None;
}
if let Ok(code) = cm.span_to_snippet(expr.span) {
Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/block-result/issue-5500.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ error[E0308]: mismatched types
LL | fn main() {
| - expected `()` because of default return type
LL | &panic!()
| ^^^^^^^^^ expected (), found reference
| ^^^^^^^^^
| |
| expected (), found reference
| help: consider removing the borrow: `panic!()`
|
= note: expected type `()`
found type `&_`
Expand Down
10 changes: 8 additions & 2 deletions src/test/ui/diverging-tuple-parts-39485.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
error[E0308]: mismatched types
--> $DIR/diverging-tuple-parts-39485.rs:8:5
|
LL | fn g() {
| - help: try adding a return type: `-> &_`
LL | &panic!() //~ ERROR mismatched types
| ^^^^^^^^^ expected (), found reference
|
= note: expected type `()`
found type `&_`
help: try adding a return type
|
LL | fn g() -> &_ {
| ^^^^^
help: consider removing the borrow
|
LL | panic!() //~ ERROR mismatched types
| ^^^^^^^^

error[E0308]: mismatched types
--> $DIR/diverging-tuple-parts-39485.rs:12:5
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/suggestions/format-borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
let a: String = &String::from("a");
//~^ ERROR mismatched types
let b: String = &format!("b");
//~^ ERROR mismatched types
}
27 changes: 27 additions & 0 deletions src/test/ui/suggestions/format-borrow.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0308]: mismatched types
--> $DIR/format-borrow.rs:2:21
|
LL | let a: String = &String::from("a");
| ^^^^^^^^^^^^^^^^^^
| |
| expected struct `std::string::String`, found reference
| help: consider removing the borrow: `String::from("a")`
|
= note: expected type `std::string::String`
found type `&std::string::String`

error[E0308]: mismatched types
--> $DIR/format-borrow.rs:4:21
|
LL | let b: String = &format!("b");
| ^^^^^^^^^^^^^
| |
| expected struct `std::string::String`, found reference
| help: consider removing the borrow: `format!("b")`
|
= note: expected type `std::string::String`
found type `&std::string::String`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.