Skip to content

Commit 1eae941

Browse files
committed
Add sub-fn for confusable or similarly named methods in FnCtxt::report_no_match_method_error
Currently this method is quiet long and complex, this commit refactors it and improves its readability by adding sub-fn
1 parent be0c04b commit 1eae941

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,43 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
953953
}
954954
}
955955

956+
fn suggest_confusable_or_similarly_named_method(
957+
&self,
958+
err: &mut Diag<'_>,
959+
span: Span,
960+
rcvr_ty: Ty<'tcx>,
961+
item_ident: Ident,
962+
mode: Mode,
963+
args: Option<&'tcx [hir::Expr<'tcx>]>,
964+
unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
965+
similar_candidate: Option<ty::AssocItem>,
966+
) {
967+
let confusable_suggested = self.confusable_method_name(
968+
err,
969+
rcvr_ty,
970+
item_ident,
971+
args.map(|args| {
972+
args.iter()
973+
.map(|expr| {
974+
self.node_ty_opt(expr.hir_id).unwrap_or_else(|| self.next_ty_var(expr.span))
975+
})
976+
.collect()
977+
}),
978+
);
979+
if let Some(similar_candidate) = similar_candidate {
980+
// Don't emit a suggestion if we found an actual method
981+
// that had unsatisfied trait bounds
982+
if unsatisfied_predicates.is_empty()
983+
// ...or if we already suggested that name because of `rustc_confusable` annotation
984+
&& Some(similar_candidate.name()) != confusable_suggested
985+
// and if we aren't in an expansion.
986+
&& !span.from_expansion()
987+
{
988+
self.find_likely_intended_associated_item(err, similar_candidate, span, args, mode);
989+
}
990+
}
991+
}
992+
956993
fn report_no_match_method_error(
957994
&self,
958995
mut span: Span,
@@ -1137,36 +1174,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11371174
source,
11381175
unsatisfied_predicates,
11391176
);
1140-
let confusable_suggested = self.confusable_method_name(
1177+
1178+
self.suggest_confusable_or_similarly_named_method(
11411179
&mut err,
1180+
span,
11421181
rcvr_ty,
11431182
item_ident,
1144-
args.map(|args| {
1145-
args.iter()
1146-
.map(|expr| {
1147-
self.node_ty_opt(expr.hir_id).unwrap_or_else(|| self.next_ty_var(expr.span))
1148-
})
1149-
.collect()
1150-
}),
1183+
mode,
1184+
args,
1185+
unsatisfied_predicates,
1186+
similar_candidate,
11511187
);
1152-
if let Some(similar_candidate) = similar_candidate {
1153-
// Don't emit a suggestion if we found an actual method
1154-
// that had unsatisfied trait bounds
1155-
if unsatisfied_predicates.is_empty()
1156-
// ...or if we already suggested that name because of `rustc_confusable` annotation
1157-
&& Some(similar_candidate.name()) != confusable_suggested
1158-
// and if we aren't in an expansion.
1159-
&& !span.from_expansion()
1160-
{
1161-
self.find_likely_intended_associated_item(
1162-
&mut err,
1163-
similar_candidate,
1164-
span,
1165-
args,
1166-
mode,
1167-
);
1168-
}
1169-
}
11701188

11711189
for (span, mut bounds) in bound_spans {
11721190
if !tcx.sess.source_map().is_span_accessible(span) {

0 commit comments

Comments
 (0)