Skip to content

Commit be0c04b

Browse files
committed
Add sub-fn to find possible candidates for method 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 2dd96c6 commit be0c04b

File tree

1 file changed

+67
-36
lines changed

1 file changed

+67
-36
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,60 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
899899
}
900900
}
901901

902+
fn find_possible_candidates_for_method(
903+
&self,
904+
err: &mut Diag<'_>,
905+
span: Span,
906+
rcvr_ty: Ty<'tcx>,
907+
item_ident: Ident,
908+
item_kind: &str,
909+
mode: Mode,
910+
source: SelfSource<'tcx>,
911+
no_match_data: &NoMatchData<'tcx>,
912+
expected: Expectation<'tcx>,
913+
should_label_not_found: bool,
914+
custom_span_label: bool,
915+
) {
916+
let mut find_candidate_for_method = false;
917+
let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
918+
919+
if should_label_not_found && !custom_span_label {
920+
self.set_not_found_span_label(
921+
err,
922+
rcvr_ty,
923+
item_ident,
924+
item_kind,
925+
mode,
926+
source,
927+
span,
928+
unsatisfied_predicates,
929+
&mut find_candidate_for_method,
930+
);
931+
}
932+
if !find_candidate_for_method {
933+
self.lookup_segments_chain_for_no_match_method(
934+
err,
935+
item_ident,
936+
item_kind,
937+
source,
938+
no_match_data,
939+
);
940+
}
941+
942+
// Don't suggest (for example) `expr.field.clone()` if `expr.clone()`
943+
// can't be called due to `typeof(expr): Clone` not holding.
944+
if unsatisfied_predicates.is_empty() {
945+
self.suggest_calling_method_on_field(
946+
err,
947+
source,
948+
span,
949+
rcvr_ty,
950+
item_ident,
951+
expected.only_has_type(self),
952+
);
953+
}
954+
}
955+
902956
fn report_no_match_method_error(
903957
&self,
904958
mut span: Span,
@@ -1032,7 +1086,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10321086
return err.emit();
10331087
}
10341088

1035-
let mut find_candidate_for_method = false;
10361089
let should_label_not_found = self.suggest_surround_method_call(
10371090
&mut err,
10381091
span,
@@ -1042,41 +1095,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10421095
&similar_candidate,
10431096
);
10441097

1045-
if should_label_not_found && !custom_span_label {
1046-
self.set_not_found_span_label(
1047-
&mut err,
1048-
rcvr_ty,
1049-
item_ident,
1050-
item_kind,
1051-
mode,
1052-
source,
1053-
span,
1054-
unsatisfied_predicates,
1055-
&mut find_candidate_for_method,
1056-
);
1057-
}
1058-
if !find_candidate_for_method {
1059-
self.lookup_segments_chain_for_no_match_method(
1060-
&mut err,
1061-
item_ident,
1062-
item_kind,
1063-
source,
1064-
no_match_data,
1065-
);
1066-
}
1067-
1068-
// Don't suggest (for example) `expr.field.clone()` if `expr.clone()`
1069-
// can't be called due to `typeof(expr): Clone` not holding.
1070-
if unsatisfied_predicates.is_empty() {
1071-
self.suggest_calling_method_on_field(
1072-
&mut err,
1073-
source,
1074-
span,
1075-
rcvr_ty,
1076-
item_ident,
1077-
expected.only_has_type(self),
1078-
);
1079-
}
1098+
self.find_possible_candidates_for_method(
1099+
&mut err,
1100+
span,
1101+
rcvr_ty,
1102+
item_ident,
1103+
item_kind,
1104+
mode,
1105+
source,
1106+
no_match_data,
1107+
expected,
1108+
should_label_not_found,
1109+
custom_span_label,
1110+
);
10801111

10811112
self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_ident);
10821113

0 commit comments

Comments
 (0)