Skip to content

Commit 32be459

Browse files
committed
Suggest replace f with f: Box<f> when expr field is short hand
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent dca57c6 commit 32be459

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

compiler/rustc_hir_typeck/src/errors.rs

+12
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,18 @@ pub(crate) enum SuggestBoxing {
680680
hir_typeck_suggest_boxing_when_appropriate,
681681
applicability = "machine-applicable"
682682
)]
683+
ExprFieldShorthand {
684+
#[suggestion_part(code = "{ident}: Box::new(")]
685+
start: Span,
686+
#[suggestion_part(code = ")")]
687+
end: Span,
688+
ident: Ident,
689+
},
690+
#[note(hir_typeck_suggest_boxing_note)]
691+
#[multipart_suggestion(
692+
hir_typeck_suggest_boxing_when_appropriate,
693+
applicability = "machine-applicable"
694+
)]
683695
Other {
684696
#[suggestion_part(code = "Box::new(")]
685697
start: Span,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+9
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
585585
{
586586
errors::SuggestBoxing::AsyncBody
587587
}
588+
_ if let Node::ExprField(expr_field) = self.tcx.parent_hir_node(hir_id)
589+
&& expr_field.is_shorthand =>
590+
{
591+
errors::SuggestBoxing::ExprFieldShorthand {
592+
start: span.shrink_to_lo(),
593+
end: span.shrink_to_hi(),
594+
ident: expr_field.ident,
595+
}
596+
}
588597
_ => errors::SuggestBoxing::Other {
589598
start: span.shrink_to_lo(),
590599
end: span.shrink_to_hi(),

tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LL | let v2 = X { a };
99
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
1010
help: store this in the heap by calling `Box::new`
1111
|
12-
LL | let v2 = X { Box::new(a) };
13-
| +++++++++ +
12+
LL | let v2 = X { a: Box::new(a) };
13+
| ++++++++++++ +
1414

1515
error[E0308]: mismatched types
1616
--> $DIR/suggest-box-for-expr-field-issue-139631.rs:12:21

0 commit comments

Comments
 (0)