typeck: extract suggest_first_deref_field
This commit is contained in:
parent
9805846881
commit
039c789325
1 changed files with 21 additions and 11 deletions
|
|
@ -1373,17 +1373,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
self.maybe_suggest_array_indexing(&mut err, expr, base, field, len);
|
||||
}
|
||||
ty::RawPtr(..) => {
|
||||
let base = self.tcx.sess.source_map()
|
||||
.span_to_snippet(base.span)
|
||||
.unwrap_or_else(|_| self.tcx.hir().hir_to_pretty_string(base.hir_id));
|
||||
let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
|
||||
let suggestion = format!("(*{}).{}", base, field);
|
||||
err.span_suggestion(
|
||||
expr.span,
|
||||
&msg,
|
||||
suggestion,
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
self.suggest_first_deref_field(&mut err, expr, base, field);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -1480,6 +1470,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
fn suggest_first_deref_field(
|
||||
&self,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
expr: &hir::Expr,
|
||||
base: &hir::Expr,
|
||||
field: ast::Ident,
|
||||
) {
|
||||
let base = self.tcx.sess.source_map()
|
||||
.span_to_snippet(base.span)
|
||||
.unwrap_or_else(|_| self.tcx.hir().hir_to_pretty_string(base.hir_id));
|
||||
let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
|
||||
let suggestion = format!("(*{}).{}", base, field);
|
||||
err.span_suggestion(
|
||||
expr.span,
|
||||
&msg,
|
||||
suggestion,
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
|
||||
fn no_such_field_err<T: Display>(&self, span: Span, field: T, expr_t: &ty::TyS<'_>)
|
||||
-> DiagnosticBuilder<'_> {
|
||||
type_error_struct!(self.tcx().sess, span, expr_t, E0609,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue