Skip to content

Commit 117d676

Browse files
committed
Add sub-fn for static method candidates 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 e8a00a7 commit 117d676

File tree

1 file changed

+71
-48
lines changed

1 file changed

+71
-48
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,66 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
712712
}
713713
}
714714

715+
fn suggest_static_method_candidates(
716+
&self,
717+
err: &mut Diag<'_>,
718+
span: Span,
719+
rcvr_ty: Ty<'tcx>,
720+
item_ident: Ident,
721+
source: SelfSource<'tcx>,
722+
args: Option<&'tcx [hir::Expr<'tcx>]>,
723+
sugg_span: Span,
724+
no_match_data: &NoMatchData<'tcx>,
725+
) -> Vec<CandidateSource> {
726+
let mut static_candidates = no_match_data.static_candidates.clone();
727+
728+
// `static_candidates` may have same candidates appended by
729+
// inherent and extension, which may result in incorrect
730+
// diagnostic.
731+
static_candidates.dedup();
732+
733+
if !static_candidates.is_empty() {
734+
err.note(
735+
"found the following associated functions; to be used as methods, \
736+
functions must have a `self` parameter",
737+
);
738+
err.span_label(span, "this is an associated function, not a method");
739+
}
740+
if static_candidates.len() == 1 {
741+
self.suggest_associated_call_syntax(
742+
err,
743+
&static_candidates,
744+
rcvr_ty,
745+
source,
746+
item_ident,
747+
args,
748+
sugg_span,
749+
);
750+
self.note_candidates_on_method_error(
751+
rcvr_ty,
752+
item_ident,
753+
source,
754+
args,
755+
span,
756+
err,
757+
&mut static_candidates,
758+
None,
759+
);
760+
} else if static_candidates.len() > 1 {
761+
self.note_candidates_on_method_error(
762+
rcvr_ty,
763+
item_ident,
764+
source,
765+
args,
766+
span,
767+
err,
768+
&mut static_candidates,
769+
Some(sugg_span),
770+
);
771+
}
772+
static_candidates
773+
}
774+
715775
fn report_no_match_method_error(
716776
&self,
717777
mut span: Span,
@@ -804,54 +864,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
804864
&mut err, span, rcvr_ty, item_ident, mode, source, expected,
805865
);
806866

807-
let mut custom_span_label = false;
808-
let mut static_candidates = no_match_data.static_candidates.clone();
809-
810-
// `static_candidates` may have same candidates appended by
811-
// inherent and extension, which may result in incorrect
812-
// diagnostic.
813-
static_candidates.dedup();
814-
815-
if !static_candidates.is_empty() {
816-
err.note(
817-
"found the following associated functions; to be used as methods, \
818-
functions must have a `self` parameter",
819-
);
820-
err.span_label(span, "this is an associated function, not a method");
821-
custom_span_label = true;
822-
}
823-
if static_candidates.len() == 1 {
824-
self.suggest_associated_call_syntax(
825-
&mut err,
826-
&static_candidates,
827-
rcvr_ty,
828-
source,
829-
item_ident,
830-
args,
831-
sugg_span,
832-
);
833-
self.note_candidates_on_method_error(
834-
rcvr_ty,
835-
item_ident,
836-
source,
837-
args,
838-
span,
839-
&mut err,
840-
&mut static_candidates,
841-
None,
842-
);
843-
} else if static_candidates.len() > 1 {
844-
self.note_candidates_on_method_error(
845-
rcvr_ty,
846-
item_ident,
847-
source,
848-
args,
849-
span,
850-
&mut err,
851-
&mut static_candidates,
852-
Some(sugg_span),
853-
);
854-
}
867+
let static_candidates = self.suggest_static_method_candidates(
868+
&mut err,
869+
span,
870+
rcvr_ty,
871+
item_ident,
872+
source,
873+
args,
874+
sugg_span,
875+
&no_match_data,
876+
);
877+
let mut custom_span_label = !static_candidates.is_empty();
855878

856879
let mut bound_spans: SortedMap<Span, Vec<String>> = Default::default();
857880
let mut restrict_type_params = false;

0 commit comments

Comments
 (0)