Skip to content

Commit

Permalink
Don't suggest dereferencing an else if expression
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed Apr 2, 2021
1 parent 0887944 commit fb7cf09
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
24 changes: 19 additions & 5 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

/// Returns whether the given expression is an `else if`.
crate fn is_else_if_block(&self, expr: &hir::Expr<'_>) -> bool {
if let hir::ExprKind::If(..) = expr.kind {
let parent_id = self.tcx.hir().get_parent_node(expr.hir_id);
if let Some(Node::Expr(hir::Expr {
kind: hir::ExprKind::If(_, _, Some(else_expr)),
..
})) = self.tcx.hir().find(parent_id)
{
return else_expr.hir_id == expr.hir_id;
}
}
false
}

/// This function is used to determine potential "simple" improvements or users' errors and
/// provide them useful help. For example:
///
Expand Down Expand Up @@ -660,12 +675,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let suggestion = if is_struct_pat_shorthand_field {
format!("{}: *{}", code, code)
} else if self.is_else_if_block(expr) {
// Don't suggest nonsense like `else *if`
return None;
} else if let Some(expr) = self.maybe_get_block_expr(expr.hir_id) {
if let Ok(inner_code) = sm.span_to_snippet(expr.span) {
format!("*{}", inner_code)
} else {
format!("*{}", code)
}
format!("*{}", sm.span_to_snippet(expr.span).unwrap_or(code))
} else {
format!("*{}", code)
};
Expand Down
10 changes: 0 additions & 10 deletions src/test/ui/deref-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,6 @@ LL | || };
| ||_____|
| |______`if` and `else` have incompatible types
| expected `i32`, found `&{integer}`
|
help: consider dereferencing the borrow
|
LL | } else *if true {
LL |
LL | b
LL | } else {
LL | &0
LL | };
|

error: aborting due to 13 previous errors

Expand Down

0 comments on commit fb7cf09

Please sign in to comment.