about summary refs log tree commit diff
diff options
context:
space:
mode:
authorxizheyin <xizheyin@smail.nju.edu.cn>2025-05-14 15:14:07 +0800
committerxizheyin <xizheyin@smail.nju.edu.cn>2025-05-14 15:14:32 +0800
commit32be4599090d62cd7f70767e27c289583fd6501d (patch)
treedf286f7805054e954920bf2270ac08b47001be23
parentdca57c6714a03fe7c7b71604c51f9b4c06ae699a (diff)
downloadrust-32be4599090d62cd7f70767e27c289583fd6501d.tar.gz
rust-32be4599090d62cd7f70767e27c289583fd6501d.zip
Suggest replace `f` with `f: Box<f>` when expr field is short hand
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
-rw-r--r--compiler/rustc_hir_typeck/src/errors.rs12
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs9
-rw-r--r--tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr4
3 files changed, 23 insertions, 2 deletions
diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs
index 73279553508..06103fe1c91 100644
--- a/compiler/rustc_hir_typeck/src/errors.rs
+++ b/compiler/rustc_hir_typeck/src/errors.rs
@@ -680,6 +680,18 @@ pub(crate) enum SuggestBoxing {
         hir_typeck_suggest_boxing_when_appropriate,
         applicability = "machine-applicable"
     )]
+    ExprFieldShorthand {
+        #[suggestion_part(code = "{ident}: Box::new(")]
+        start: Span,
+        #[suggestion_part(code = ")")]
+        end: Span,
+        ident: Ident,
+    },
+    #[note(hir_typeck_suggest_boxing_note)]
+    #[multipart_suggestion(
+        hir_typeck_suggest_boxing_when_appropriate,
+        applicability = "machine-applicable"
+    )]
     Other {
         #[suggestion_part(code = "Box::new(")]
         start: Span,
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
index 91eb1989864..251801f479e 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
@@ -585,6 +585,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 {
                     errors::SuggestBoxing::AsyncBody
                 }
+                _ if let Node::ExprField(expr_field) = self.tcx.parent_hir_node(hir_id)
+                    && expr_field.is_shorthand =>
+                {
+                    errors::SuggestBoxing::ExprFieldShorthand {
+                        start: span.shrink_to_lo(),
+                        end: span.shrink_to_hi(),
+                        ident: expr_field.ident,
+                    }
+                }
                 _ => errors::SuggestBoxing::Other {
                     start: span.shrink_to_lo(),
                     end: span.shrink_to_hi(),
diff --git a/tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr b/tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr
index 167e892d11a..01bd0523a16 100644
--- a/tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr
+++ b/tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr
@@ -9,8 +9,8 @@ LL |     let v2 = X { a };
    = 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
 help: store this in the heap by calling `Box::new`
    |
-LL |     let v2 = X { Box::new(a) };
-   |                  +++++++++ +
+LL |     let v2 = X { a: Box::new(a) };
+   |                  ++++++++++++ +
 
 error[E0308]: mismatched types
   --> $DIR/suggest-box-for-expr-field-issue-139631.rs:12:21