Skip to content

Suggest removing the tuple struct field for the unwrapped value #99593

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 1 commit into from
Jul 26, 2022
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
15 changes: 15 additions & 0 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr_ty: Ty<'tcx>,
) {
if let ty::Adt(expected_adt, substs) = expected.kind() {
if let hir::ExprKind::Field(base, ident) = expr.kind {
let base_ty = self.typeck_results.borrow().expr_ty(base);
if self.can_eq(self.param_env, base_ty, expected).is_ok()
&& let Some(base_span) = base.span.find_ancestor_inside(expr.span)
{
err.span_suggestion_verbose(
expr.span.with_lo(base_span.hi()),
format!("consider removing the tuple struct field `{ident}`"),
"",
Applicability::MaybeIncorrect,
);
return
}
}

// If the expression is of type () and it's the return expression of a block,
// we suggest adding a separate return expression instead.
// (To avoid things like suggesting `Ok(while .. { .. })`.)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// run-rustfix

macro_rules! my_wrapper {
($expr:expr) => { MyWrapper($expr) }
}

pub struct MyWrapper(u32);

fn main() {
let value = MyWrapper(123);
some_fn(value); //~ ERROR mismatched types
some_fn(my_wrapper!(123)); //~ ERROR mismatched types
}

fn some_fn(wrapped: MyWrapper) {
drop(wrapped);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// run-rustfix

macro_rules! my_wrapper {
($expr:expr) => { MyWrapper($expr) }
}

pub struct MyWrapper(u32);

fn main() {
let value = MyWrapper(123);
some_fn(value.0); //~ ERROR mismatched types
some_fn(my_wrapper!(123).0); //~ ERROR mismatched types
}

fn some_fn(wrapped: MyWrapper) {
drop(wrapped);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
error[E0308]: mismatched types
--> $DIR/suggest-removing-tulpe-struct-field.rs:11:13
|
LL | some_fn(value.0);
| ------- ^^^^^^^ expected struct `MyWrapper`, found `u32`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/suggest-removing-tulpe-struct-field.rs:15:4
|
LL | fn some_fn(wrapped: MyWrapper) {
| ^^^^^^^ ------------------
help: consider removing the tuple struct field `0`
|
LL - some_fn(value.0);
LL + some_fn(value);
|

error[E0308]: mismatched types
--> $DIR/suggest-removing-tulpe-struct-field.rs:12:13
|
LL | some_fn(my_wrapper!(123).0);
| ------- ^^^^^^^^^^^^^^^^^^ expected struct `MyWrapper`, found `u32`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/suggest-removing-tulpe-struct-field.rs:15:4
|
LL | fn some_fn(wrapped: MyWrapper) {
| ^^^^^^^ ------------------
help: consider removing the tuple struct field `0`
|
LL - some_fn(my_wrapper!(123).0);
LL + some_fn(my_wrapper!(123));
|

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.