Skip to content

Suggest replace f with f: Box<f> when expr field is short hand #140989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/box/suggest-box-for-expr-field-issue-139631.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct X {
a: Box<u32>,
}

struct Y {
y: Box<u32>,
}

fn main() {
let a = 8;
let v2 = X { a }; //~ ERROR mismatched types [E0308]
let v3 = Y { y: a }; //~ ERROR mismatched types [E0308]
let v4 = Y { a }; //~ ERROR struct `Y` has no field named `a` [E0560]
}
44 changes: 44 additions & 0 deletions tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
error[E0308]: mismatched types
--> $DIR/suggest-box-for-expr-field-issue-139631.rs:11:18
|
LL | let v2 = X { a };
| ^ expected `Box<u32>`, found integer
|
= note: expected struct `Box<u32>`
found type `{integer}`
= 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 { a: Box::new(a) };
| ++++++++++++ +

error[E0308]: mismatched types
--> $DIR/suggest-box-for-expr-field-issue-139631.rs:12:21
|
LL | let v3 = Y { y: a };
| ^ expected `Box<u32>`, found integer
|
= note: expected struct `Box<u32>`
found type `{integer}`
= 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 v3 = Y { y: Box::new(a) };
| +++++++++ +

error[E0560]: struct `Y` has no field named `a`
--> $DIR/suggest-box-for-expr-field-issue-139631.rs:13:18
|
LL | let v4 = Y { a };
| ^ unknown field
|
help: a field with a similar name exists
|
LL - let v4 = Y { a };
LL + let v4 = Y { y };
|

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0308, E0560.
For more information about an error, try `rustc --explain E0308`.
Loading