Skip to content

Commit 9fddfc4

Browse files
authored
Unrolled build for #146259
Rollup merge of #146259 - camsteffen:remove-the-box, r=jackh726 Suggest removing Box::new instead of unboxing it
2 parents ba4b643 + d66fb49 commit 9fddfc4

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,6 +3021,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30213021
)
30223022
{
30233023
let deref_kind = if checked_ty.is_box() {
3024+
// detect Box::new(..)
3025+
if let ExprKind::Call(box_new, [_]) = expr.kind
3026+
&& let ExprKind::Path(qpath) = &box_new.kind
3027+
&& let Res::Def(DefKind::AssocFn, fn_id) =
3028+
self.typeck_results.borrow().qpath_res(qpath, box_new.hir_id)
3029+
&& let Some(impl_id) = self.tcx.inherent_impl_of_assoc(fn_id)
3030+
&& self.tcx.type_of(impl_id).skip_binder().is_box()
3031+
&& self.tcx.item_name(fn_id) == sym::new
3032+
{
3033+
let l_paren = self.tcx.sess.source_map().next_point(box_new.span);
3034+
let r_paren = self.tcx.sess.source_map().end_point(expr.span);
3035+
return Some((
3036+
vec![
3037+
(box_new.span.to(l_paren), String::new()),
3038+
(r_paren, String::new()),
3039+
],
3040+
"consider removing the Box".to_string(),
3041+
Applicability::MachineApplicable,
3042+
false,
3043+
false,
3044+
));
3045+
}
30243046
"unboxing the value"
30253047
} else if checked_ty.is_ref() {
30263048
"dereferencing the borrow"

tests/ui/coercion/coerce-block-tail.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ LL | let _: &i32 = & { Box::new(1i32) };
66
|
77
= note: expected type `i32`
88
found struct `Box<i32>`
9-
help: consider unboxing the value
9+
help: consider removing the Box
10+
|
11+
LL - let _: &i32 = & { Box::new(1i32) };
12+
LL + let _: &i32 = & { 1i32 };
1013
|
11-
LL | let _: &i32 = & { *Box::new(1i32) };
12-
| +
1314

1415
error: aborting due to 1 previous error
1516

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let _: String = Box::new(String::new());
3+
//~^ ERROR mismatched types
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/coerce-box-new-to-unboxed.rs:2:21
3+
|
4+
LL | let _: String = Box::new(String::new());
5+
| ------ ^^^^^^^^^^^^^^^^^^^^^^^ expected `String`, found `Box<String>`
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected struct `String`
10+
found struct `Box<String>`
11+
help: consider removing the Box
12+
|
13+
LL - let _: String = Box::new(String::new());
14+
LL + let _: String = String::new();
15+
|
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)