mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 15:23:46 +00:00
suggest dereferencing receiver arguments properly
fix a stderr
This commit is contained in:
parent
aa5b179599
commit
19fa5b381c
@ -694,7 +694,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||
trait_pred: ty::PolyTraitPredicate<'tcx>,
|
||||
) -> bool {
|
||||
// It only make sense when suggesting dereferences for arguments
|
||||
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code()
|
||||
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, call_hir_id, .. } = obligation.cause.code()
|
||||
else { return false; };
|
||||
let Some(typeck_results) = &self.typeck_results
|
||||
else { return false; };
|
||||
@ -773,12 +773,33 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||
real_trait_pred_and_base_ty,
|
||||
);
|
||||
if self.predicate_may_hold(&obligation) {
|
||||
err.span_suggestion_verbose(
|
||||
span.shrink_to_lo(),
|
||||
"consider dereferencing here",
|
||||
"*",
|
||||
Applicability::MachineApplicable,
|
||||
let call_node = self.tcx.hir().get(*call_hir_id);
|
||||
let msg = "consider dereferencing here";
|
||||
let is_receiver = matches!(
|
||||
call_node,
|
||||
Node::Expr(hir::Expr {
|
||||
kind: hir::ExprKind::MethodCall(_, receiver_expr, ..),
|
||||
..
|
||||
})
|
||||
if receiver_expr.hir_id == *arg_hir_id
|
||||
);
|
||||
if is_receiver {
|
||||
err.multipart_suggestion_verbose(
|
||||
msg,
|
||||
vec![
|
||||
(span.shrink_to_lo(), "(*".to_string()),
|
||||
(span.shrink_to_hi(), ")".to_string()),
|
||||
],
|
||||
Applicability::MachineApplicable,
|
||||
)
|
||||
} else {
|
||||
err.span_suggestion_verbose(
|
||||
span.shrink_to_lo(),
|
||||
msg,
|
||||
'*',
|
||||
Applicability::MachineApplicable,
|
||||
)
|
||||
};
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -2857,6 +2878,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||
arg_hir_id,
|
||||
call_hir_id,
|
||||
ref parent_code,
|
||||
..
|
||||
} => {
|
||||
self.function_argument_obligation(
|
||||
arg_hir_id,
|
||||
|
@ -0,0 +1,14 @@
|
||||
// run-rustfix
|
||||
|
||||
struct TargetStruct;
|
||||
|
||||
impl From<usize> for TargetStruct {
|
||||
fn from(_unchecked: usize) -> Self {
|
||||
TargetStruct
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = &3;
|
||||
let _b: TargetStruct = (*a).into(); //~ ERROR the trait bound `TargetStruct: From<&{integer}>` is not satisfied
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
// run-rustfix
|
||||
|
||||
struct TargetStruct;
|
||||
|
||||
impl From<usize> for TargetStruct {
|
||||
fn from(_unchecked: usize) -> Self {
|
||||
TargetStruct
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = &3;
|
||||
let _b: TargetStruct = a.into(); //~ ERROR the trait bound `TargetStruct: From<&{integer}>` is not satisfied
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
error[E0277]: the trait bound `TargetStruct: From<&{integer}>` is not satisfied
|
||||
--> $DIR/suggest-dereferencing-receiver-argument.rs:13:30
|
||||
|
|
||||
LL | let _b: TargetStruct = a.into();
|
||||
| ^^^^ the trait `From<&{integer}>` is not implemented for `TargetStruct`
|
||||
|
|
||||
= note: required for `&{integer}` to implement `Into<TargetStruct>`
|
||||
help: consider dereferencing here
|
||||
|
|
||||
LL | let _b: TargetStruct = (*a).into();
|
||||
| ++ +
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0277`.
|
Loading…
Reference in New Issue
Block a user