Skip to content

Commit 56ce33f

Browse files
committed
Add sub-fn for surround method calls 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 f23675f commit 56ce33f

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
886886
))
887887
}
888888

889+
fn suggest_surround_method_call(
890+
&self,
891+
err: &mut Diag<'_>,
892+
span: Span,
893+
rcvr_ty: Ty<'tcx>,
894+
item_ident: Ident,
895+
source: SelfSource<'tcx>,
896+
similar_candidate: &Option<ty::AssocItem>,
897+
) -> bool {
898+
match source {
899+
// If the method name is the name of a field with a function or closure type,
900+
// give a helping note that it has to be called as `(x.f)(...)`.
901+
SelfSource::MethodCall(expr) => {
902+
!self.suggest_calling_field_as_fn(span, rcvr_ty, expr, item_ident, err)
903+
&& similar_candidate.is_none()
904+
}
905+
_ => true,
906+
}
907+
}
908+
889909
fn report_no_match_method_error(
890910
&self,
891911
mut span: Span,
@@ -1018,15 +1038,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10181038
};
10191039

10201040
let mut find_candidate_for_method = false;
1021-
let should_label_not_found = match source {
1022-
// If the method name is the name of a field with a function or closure type,
1023-
// give a helping note that it has to be called as `(x.f)(...)`.
1024-
SelfSource::MethodCall(expr) => {
1025-
!self.suggest_calling_field_as_fn(span, rcvr_ty, expr, item_ident, &mut err)
1026-
&& similar_candidate.is_none()
1027-
}
1028-
_ => true,
1029-
};
1041+
let should_label_not_found = self.suggest_surround_method_call(
1042+
&mut err,
1043+
span,
1044+
rcvr_ty,
1045+
item_ident,
1046+
source,
1047+
&similar_candidate,
1048+
);
10301049

10311050
if should_label_not_found && !custom_span_label {
10321051
self.set_not_found_span_label(

0 commit comments

Comments
 (0)