Skip to content
Merged
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
42 changes: 11 additions & 31 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let methods =
self.get_conversion_methods_for_diagnostic(expr.span, expected, found, expr.hir_id);

if let Some((suggestion, msg, applicability, verbose, annotation)) =
if let Some((suggestion, msg, applicability, annotation)) =
self.suggest_deref_or_ref(expr, found, expected)
{
if verbose {
err.multipart_suggestion(msg, suggestion, applicability);
} else {
err.multipart_suggestion(msg, suggestion, applicability);
}
err.multipart_suggestion(msg, suggestion, applicability);
if annotation {
let suggest_annotation = match expr.peel_drop_temps().kind {
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mutbl, _) => mutbl.ref_prefix_str(),
Expand Down Expand Up @@ -2918,7 +2914,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Vec<(Span, String)>,
String,
Applicability,
bool, /* verbose */
bool, /* suggest `&` or `&mut` type annotation */
)> {
let sess = self.sess();
Expand Down Expand Up @@ -2949,7 +2944,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
vec![(sp.with_hi(pos), String::new())],
"consider removing the leading `b`".to_string(),
Applicability::MachineApplicable,
true,
false,
));
}
Expand All @@ -2963,7 +2957,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
vec![(sp.shrink_to_lo(), "b".to_string())],
"consider adding a leading `b`".to_string(),
Applicability::MachineApplicable,
true,
false,
));
}
Expand Down Expand Up @@ -3020,7 +3013,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
vec![(sugg_sp, String::new())],
"consider removing deref here".to_string(),
Applicability::MachineApplicable,
true,
false,
));
}
Expand All @@ -3035,7 +3027,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// If the block does not have a final expression, it will return () and we do not make a suggestion to borrow that.
let ExprKind::Block(then, _) = then.kind else { return None };
let Some(then) = then.expr else { return None };
let (mut suggs, help, app, verbose, mutref) =
let (mut suggs, help, app, mutref) =
self.suggest_deref_or_ref(then, checked_ty, expected)?;

// If there is no `else`, the return type of this `if` will be (), so suggesting to change the `then` block is useless
Expand All @@ -3047,15 +3039,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.suggest_deref_or_ref(els_expr, checked_ty, expected)?;
suggs.extend(else_suggs);

return Some((suggs, help, app, verbose, mutref));
return Some((suggs, help, app, mutref));
}

if let Some((sugg, msg)) = self.can_use_as_ref(expr) {
return Some((
sugg,
msg.to_string(),
Applicability::MachineApplicable,
true,
false,
));
}
Expand All @@ -3076,15 +3067,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let make_sugg = |expr: &Expr<'_>, span: Span, sugg: &str| {
if expr_needs_parens(expr) {
(
vec![
(span.shrink_to_lo(), format!("{prefix}{sugg}(")),
(span.shrink_to_hi(), ")".to_string()),
],
false,
)
vec![
(span.shrink_to_lo(), format!("{prefix}{sugg}(")),
(span.shrink_to_hi(), ")".to_string()),
]
} else {
(vec![(span.shrink_to_lo(), format!("{prefix}{sugg}"))], true)
vec![(span.shrink_to_lo(), format!("{prefix}{sugg}"))]
}
};

Expand All @@ -3095,24 +3083,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}) = self.tcx.parent_hir_node(expr.hir_id)
&& let &ty::Ref(..) = self.check_expr(lhs).kind()
{
let (sugg, verbose) = make_sugg(lhs, lhs.span, "*");
let sugg = make_sugg(lhs, lhs.span, "*");

return Some((
sugg,
"consider dereferencing the borrow".to_string(),
Applicability::MachineApplicable,
verbose,
false,
));
}

let sugg = mutability.ref_prefix_str();
let (sugg, verbose) = make_sugg(expr, sp, sugg);
let sugg = make_sugg(expr, sp, sugg);
return Some((
sugg,
format!("consider {}borrowing here", mutability.mutably_str()),
Applicability::MachineApplicable,
verbose,
false,
));
}
Expand All @@ -3131,7 +3117,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"consider removing the borrow".to_string(),
Applicability::MachineApplicable,
true,
true,
))
};

Expand Down Expand Up @@ -3194,7 +3179,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
vec![(span, src)],
"consider dereferencing".to_string(),
applicability,
true,
false,
));
}
Expand Down Expand Up @@ -3231,7 +3215,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
Applicability::MachineApplicable
},
true,
false,
));
}
Expand Down Expand Up @@ -3267,7 +3250,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"consider removing the Box".to_string(),
Applicability::MachineApplicable,
false,
false,
));
}
"unboxing the value"
Expand Down Expand Up @@ -3316,7 +3298,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
],
message,
Applicability::MachineApplicable,
true,
false,
));
}
Expand All @@ -3325,7 +3306,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
vec![(span, suggestion)],
message,
Applicability::MachineApplicable,
true,
false,
));
}
Expand Down
Loading