Skip to content

Commit a9051d8

Browse files
Tweak borrow suggestion
1 parent ad6b20b commit a9051d8

26 files changed

+288
-211
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+42-39
Original file line numberDiff line numberDiff line change
@@ -1334,52 +1334,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13341334
));
13351335
}
13361336

1337-
if let Ok(src) = sm.span_to_snippet(sugg_sp) {
1338-
let needs_parens = match expr.kind {
1339-
// parenthesize if needed (Issue #46756)
1340-
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
1341-
// parenthesize borrows of range literals (Issue #54505)
1342-
_ if is_range_literal(expr) => true,
1343-
_ => false,
1344-
};
1345-
1346-
if let Some(sugg) = self.can_use_as_ref(expr) {
1347-
return Some((
1348-
sugg.0,
1349-
sugg.1.to_string(),
1350-
sugg.2,
1351-
Applicability::MachineApplicable,
1352-
false,
1353-
false,
1354-
));
1355-
}
1356-
1357-
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
1358-
Some(ident) => format!("{ident}: "),
1359-
None => String::new(),
1360-
};
1361-
1362-
if let Some(hir::Node::Expr(hir::Expr {
1363-
kind: hir::ExprKind::Assign(..),
1364-
..
1365-
})) = self.tcx.hir().find_parent(expr.hir_id)
1366-
{
1367-
if mutability.is_mut() {
1368-
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
1369-
return None;
1370-
}
1371-
}
1337+
let needs_parens = match expr.kind {
1338+
// parenthesize if needed (Issue #46756)
1339+
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
1340+
// parenthesize borrows of range literals (Issue #54505)
1341+
_ if is_range_literal(expr) => true,
1342+
_ => false,
1343+
};
13721344

1373-
let sugg_expr = if needs_parens { format!("({src})") } else { src };
1345+
if let Some(sugg) = self.can_use_as_ref(expr) {
13741346
return Some((
1375-
sp,
1376-
format!("consider {}borrowing here", mutability.mutably_str()),
1377-
format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()),
1347+
sugg.0,
1348+
sugg.1.to_string(),
1349+
sugg.2,
13781350
Applicability::MachineApplicable,
13791351
false,
13801352
false,
13811353
));
13821354
}
1355+
1356+
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
1357+
Some(ident) => format!("{ident}: "),
1358+
None => String::new(),
1359+
};
1360+
1361+
if let Some(hir::Node::Expr(hir::Expr {
1362+
kind: hir::ExprKind::Assign(..),
1363+
..
1364+
})) = self.tcx.hir().find_parent(expr.hir_id)
1365+
{
1366+
if mutability.is_mut() {
1367+
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
1368+
return None;
1369+
}
1370+
}
1371+
1372+
let (sp, sugg_expr, verbose) = if needs_parens {
1373+
let src = sm.span_to_snippet(sugg_sp).ok()?;
1374+
(sp, format!("({src})"), false)
1375+
} else {
1376+
(sp.shrink_to_lo(), "".to_string(), true)
1377+
};
1378+
return Some((
1379+
sp,
1380+
format!("consider {}borrowing here", mutability.mutably_str()),
1381+
format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()),
1382+
Applicability::MachineApplicable,
1383+
verbose,
1384+
false,
1385+
));
13831386
}
13841387
}
13851388
(

tests/ui/argument-suggestions/issue-97484.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | fn foo(a: &A, d: D, e: &E, g: G) {}
1616
help: consider borrowing here
1717
|
1818
LL | foo(&&A, B, C, D, &E, F, G);
19-
| ~~
19+
| +
2020
help: remove the extra arguments
2121
|
2222
LL - foo(&&A, B, C, D, E, F, G);

tests/ui/async-await/issues/issue-102206.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ error[E0308]: mismatched types
22
--> $DIR/issue-102206.rs:6:27
33
|
44
LL | std::mem::size_of_val(foo());
5-
| --------------------- ^^^^^
6-
| | |
7-
| | expected `&_`, found future
8-
| | help: consider borrowing here: `&foo()`
5+
| --------------------- ^^^^^ expected `&_`, found future
6+
| |
97
| arguments to this function are incorrect
108
|
119
note: function defined here
1210
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
11+
help: consider borrowing here
12+
|
13+
LL | std::mem::size_of_val(&foo());
14+
| +
1315

1416
error: aborting due to previous error
1517

tests/ui/coercion/coercion-slice.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ error[E0308]: mismatched types
22
--> $DIR/coercion-slice.rs:4:21
33
|
44
LL | let _: &[i32] = [0];
5-
| ------ ^^^
6-
| | |
7-
| | expected `&[i32]`, found `[{integer}; 1]`
8-
| | help: consider borrowing here: `&[0]`
5+
| ------ ^^^ expected `&[i32]`, found `[{integer}; 1]`
6+
| |
97
| expected due to this
8+
|
9+
help: consider borrowing here
10+
|
11+
LL | let _: &[i32] = &[0];
12+
| +
1013

1114
error: aborting due to previous error
1215

tests/ui/inference/deref-suggestion.stderr

+12-8
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,23 @@ error[E0308]: mismatched types
9898
--> $DIR/deref-suggestion.rs:40:17
9999
|
100100
LL | let s = S { u };
101-
| ^
102-
| |
103-
| expected `&u32`, found integer
104-
| help: consider borrowing here: `u: &u`
101+
| ^ expected `&u32`, found integer
102+
|
103+
help: consider borrowing here
104+
|
105+
LL | let s = S { u: &u };
106+
| ++++
105107

106108
error[E0308]: mismatched types
107109
--> $DIR/deref-suggestion.rs:42:20
108110
|
109111
LL | let s = S { u: u };
110-
| ^
111-
| |
112-
| expected `&u32`, found integer
113-
| help: consider borrowing here: `&u`
112+
| ^ expected `&u32`, found integer
113+
|
114+
help: consider borrowing here
115+
|
116+
LL | let s = S { u: &u };
117+
| +
114118

115119
error[E0308]: mismatched types
116120
--> $DIR/deref-suggestion.rs:45:17

tests/ui/issues/issue-11374.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ error[E0308]: mismatched types
22
--> $DIR/issue-11374.rs:26:15
33
|
44
LL | c.read_to(v);
5-
| ------- ^
6-
| | |
7-
| | expected `&mut [u8]`, found `Vec<_>`
8-
| | help: consider mutably borrowing here: `&mut v`
5+
| ------- ^ expected `&mut [u8]`, found `Vec<_>`
6+
| |
97
| arguments to this method are incorrect
108
|
119
= note: expected mutable reference `&mut [u8]`
@@ -15,6 +13,10 @@ note: method defined here
1513
|
1614
LL | pub fn read_to(&mut self, vec: &mut [u8]) {
1715
| ^^^^^^^ --------------
16+
help: consider mutably borrowing here
17+
|
18+
LL | c.read_to(&mut v);
19+
| ++++
1820

1921
error: aborting due to previous error
2022

tests/ui/issues/issue-17033.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ error[E0308]: mismatched types
22
--> $DIR/issue-17033.rs:2:10
33
|
44
LL | (*p)(())
5-
| ---- ^^
6-
| | |
7-
| | expected `&mut ()`, found `()`
8-
| | help: consider mutably borrowing here: `&mut ()`
5+
| ---- ^^ expected `&mut ()`, found `()`
6+
| |
97
| arguments to this function are incorrect
8+
|
9+
help: consider mutably borrowing here
10+
|
11+
LL | (*p)(&mut ())
12+
| ++++
1013

1114
error: aborting due to previous error
1215

tests/ui/issues/issue-18819.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | fn print_x(_: &dyn Foo<Item=bool>, extra: &str) {
1919
help: consider borrowing here
2020
|
2121
LL | print_x(&X);
22-
| ~~
22+
| +
2323
help: provide the argument
2424
|
2525
LL | print_x(/* &dyn Foo<Item = bool> */, /* &str */);

tests/ui/issues/issue-46302.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ error[E0308]: mismatched types
22
--> $DIR/issue-46302.rs:3:27
33
|
44
LL | let u: &str = if true { s[..2] } else { s };
5-
| ^^^^^^
6-
| |
7-
| expected `&str`, found `str`
8-
| help: consider borrowing here: `&s[..2]`
5+
| ^^^^^^ expected `&str`, found `str`
6+
|
7+
help: consider borrowing here
8+
|
9+
LL | let u: &str = if true { &s[..2] } else { s };
10+
| +
911

1012
error: aborting due to previous error
1113

tests/ui/issues/issue-61106.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0308]: mismatched types
22
--> $DIR/issue-61106.rs:3:9
33
|
44
LL | foo(x.clone());
5-
| --- ^^^^^^^^^
6-
| | |
7-
| | expected `&str`, found `String`
8-
| | help: consider borrowing here: `&x`
5+
| --- ^^^^^^^^^ expected `&str`, found `String`
6+
| |
97
| arguments to this function are incorrect
108
|
119
note: function defined here
1210
--> $DIR/issue-61106.rs:6:4
1311
|
1412
LL | fn foo(_: &str) {}
1513
| ^^^ -------
14+
help: consider borrowing here
15+
|
16+
LL | foo(&x.clone());
17+
| +
1618

1719
error: aborting due to previous error
1820

tests/ui/methods/method-self-arg-1.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0308]: mismatched types
22
--> $DIR/method-self-arg-1.rs:11:14
33
|
44
LL | Foo::bar(x);
5-
| -------- ^
6-
| | |
7-
| | expected `&Foo`, found `Foo`
8-
| | help: consider borrowing here: `&x`
5+
| -------- ^ expected `&Foo`, found `Foo`
6+
| |
97
| arguments to this function are incorrect
108
|
119
note: method defined here
1210
--> $DIR/method-self-arg-1.rs:6:8
1311
|
1412
LL | fn bar(&self) {}
1513
| ^^^ -----
14+
help: consider borrowing here
15+
|
16+
LL | Foo::bar(&x);
17+
| +
1618

1719
error[E0308]: mismatched types
1820
--> $DIR/method-self-arg-1.rs:13:14

tests/ui/mismatched_types/dont-point-return-on-E0308.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0308]: mismatched types
22
--> $DIR/dont-point-return-on-E0308.rs:11:11
33
|
44
LL | f(());
5-
| - ^^
6-
| | |
7-
| | expected `&()`, found `()`
8-
| | help: consider borrowing here: `&()`
5+
| - ^^ expected `&()`, found `()`
6+
| |
97
| arguments to this function are incorrect
108
|
119
note: function defined here
1210
--> $DIR/dont-point-return-on-E0308.rs:3:10
1311
|
1412
LL | async fn f(_: &()) {}
1513
| ^ ------
14+
help: consider borrowing here
15+
|
16+
LL | f(&());
17+
| +
1618

1719
error: aborting due to previous error
1820

tests/ui/mut/mut-cross-borrowing.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ error[E0308]: mismatched types
22
--> $DIR/mut-cross-borrowing.rs:7:7
33
|
44
LL | f(x)
5-
| - ^
6-
| | |
7-
| | expected `&mut isize`, found `Box<{integer}>`
8-
| | help: consider mutably borrowing here: `&mut x`
5+
| - ^ expected `&mut isize`, found `Box<{integer}>`
6+
| |
97
| arguments to this function are incorrect
108
|
119
= note: expected mutable reference `&mut isize`
@@ -15,6 +13,10 @@ note: function defined here
1513
|
1614
LL | fn f(_: &mut isize) {}
1715
| ^ -------------
16+
help: consider mutably borrowing here
17+
|
18+
LL | f(&mut x)
19+
| ++++
1820

1921
error: aborting due to previous error
2022

0 commit comments

Comments
 (0)