Skip to content

Properly suggest deref in else block #79755

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

Closed
wants to merge 2 commits into from
Closed
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 @@ -360,6 +360,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
false
}

crate fn hir_id_sole_block_element(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
crate fn hir_id_sole_block_element(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> {
crate fn maybe_get_block_expr(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> {

Could you add a doc comment too?

match self.tcx.hir().find(hir_id)? {
Node::Expr(hir::Expr { kind: hir::ExprKind::Block(block, ..), .. }) => block.expr,
_ => None,
}
}

/// This function is used to determine potential "simple" improvements or users' errors and
/// provide them useful help. For example:
///
Expand Down Expand Up @@ -626,6 +633,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let suggestion = if is_struct_pat_shorthand_field {
format!("{}: *{}", code, code)
} else if let Some(expr) =
self.hir_id_sole_block_element(expr.hir_id)
{
if let Ok(inner_code) = sm.span_to_snippet(expr.span) {
format!("*{}", inner_code)
} else {
format!("*{}", code)
}
} else {
format!("*{}", code)
};
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/deref-suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,22 @@ fn main() {
//~^ ERROR mismatched types
let r = R { i: i };
//~^ ERROR mismatched types


let a = &1;
let b = &2;
let val: i32 = if true {
a + 1
} else {
b
//~^ ERROR mismatched types
};
let val: i32 = if true {
let _ = 2;
a + 1
} else {
let _ = 2;
b
//~^ ERROR mismatched types
};
}
20 changes: 19 additions & 1 deletion src/test/ui/deref-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ LL | let r = R { i: i };
| expected `u32`, found `&{integer}`
| help: consider dereferencing the borrow: `*i`

error: aborting due to 10 previous errors
error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:55:9
|
LL | b
| ^
| |
| expected `i32`, found `&{integer}`
| help: consider dereferencing the borrow: `*b`

error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:63:9
|
LL | b
| ^
| |
| expected `i32`, found `&{integer}`
| help: consider dereferencing the borrow: `*b`

error: aborting due to 12 previous errors

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