Skip to content

Commit 3537658

Browse files
committed
Auto merge of #46761 - zackmdavis:concerning_incorrect_suggestions_for_referencing_a_cast, r=estebank
in which suggestions to borrow casts or binary expressions are rectified resolves #46756 r? @estebank
2 parents 1b1c792 + 73a9019 commit 3537658

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/librustc_typeck/check/demand.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
260260
// Use the callsite's span if this is a macro call. #41858
261261
let sp = self.sess().codemap().call_span_if_macro(expr.span);
262262
if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(sp) {
263+
let sugg_expr = match expr.node { // parenthesize if needed (Issue #46756)
264+
hir::ExprCast(_, _) | hir::ExprBinary(_, _, _) => format!("({})", src),
265+
_ => src,
266+
};
263267
return Some(match mutability.mutbl {
264268
hir::Mutability::MutMutable => {
265-
("consider mutably borrowing here", format!("&mut {}", src))
269+
("consider mutably borrowing here", format!("&mut {}", sugg_expr))
266270
}
267271
hir::Mutability::MutImmutable => {
268-
("consider borrowing here", format!("&{}", src))
272+
("consider borrowing here", format!("&{}", sugg_expr))
269273
}
270274
});
271275
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(unused)]
12+
13+
fn light_flows_our_war_of_mocking_words(and_yet: &usize) -> usize {
14+
and_yet + 1
15+
}
16+
17+
fn main() {
18+
let behold: isize = 2;
19+
let with_tears: usize = 3;
20+
light_flows_our_war_of_mocking_words(behold as usize);
21+
//~^ ERROR mismatched types [E0308]
22+
light_flows_our_war_of_mocking_words(with_tears + 4);
23+
//~^ ERROR mismatched types [E0308]
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:20:42
3+
|
4+
20 | light_flows_our_war_of_mocking_words(behold as usize);
5+
| ^^^^^^^^^^^^^^^
6+
| |
7+
| expected &usize, found usize
8+
| help: consider borrowing here: `&(behold as usize)`
9+
|
10+
= note: expected type `&usize`
11+
found type `usize`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:22:42
15+
|
16+
22 | light_flows_our_war_of_mocking_words(with_tears + 4);
17+
| ^^^^^^^^^^^^^^
18+
| |
19+
| expected &usize, found usize
20+
| help: consider borrowing here: `&(with_tears + 4)`
21+
|
22+
= note: expected type `&usize`
23+
found type `usize`
24+
25+
error: aborting due to 2 previous errors
26+

0 commit comments

Comments
 (0)