Skip to content

Commit b368229

Browse files
committed
review comment: use early return
1 parent 3c872e2 commit b368229

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/librustc_middle/ty/error.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ impl<T> Trait<T> for X {
609609
"consider constraining the associated type `{}` to `{}`",
610610
values.expected, values.found
611611
);
612-
let mut suggested = false;
613612
let body_owner = self.hir().get_if_local(body_owner_def_id);
614613
let current_method_ident = body_owner.and_then(|n| n.ident()).map(|i| i.name);
615614

@@ -634,7 +633,10 @@ impl<T> Trait<T> for X {
634633
// type error is a comparison of an `impl` with its `trait` or when the
635634
// scope is outside of a `Body`.
636635
} else {
637-
suggested |= self.point_at_methods_that_satisfy_associated_type(
636+
// If we find a suitable associated function that returns the expected type, we don't
637+
// want the more general suggestion later in this method about "consider constraining
638+
// the associated type or calling a method that returns the associated type".
639+
let point_at_assoc_fn = self.point_at_methods_that_satisfy_associated_type(
638640
db,
639641
assoc.container.id(),
640642
current_method_ident,
@@ -643,25 +645,32 @@ impl<T> Trait<T> for X {
643645
);
644646
// Possibly suggest constraining the associated type to conform to the
645647
// found type.
646-
suggested |=
647-
self.suggest_constraint(db, &msg, body_owner_def_id, proj_ty, values.found);
648-
}
649-
if !suggested {
650-
suggested = self.point_at_associated_type(db, body_owner_def_id, values.found);
648+
if self.suggest_constraint(db, &msg, body_owner_def_id, proj_ty, values.found)
649+
|| point_at_assoc_fn
650+
{
651+
return;
652+
}
651653
}
654+
652655
if let ty::Opaque(def_id, _) = proj_ty.self_ty().kind {
653656
// When the expected `impl Trait` is not defined in the current item, it will come from
654657
// a return type. This can occur when dealing with `TryStream` (#71035).
655-
suggested |= self.constrain_associated_type_structured_suggestion(
658+
if self.constrain_associated_type_structured_suggestion(
656659
db,
657660
self.def_span(def_id),
658661
&assoc,
659662
values.found,
660663
&msg,
661-
);
664+
) {
665+
return;
666+
}
667+
}
668+
669+
if self.point_at_associated_type(db, body_owner_def_id, values.found) {
670+
return;
662671
}
663672

664-
if !suggested && !impl_comparison {
673+
if !impl_comparison {
665674
// Generic suggestion when we can't be more specific.
666675
if callable_scope {
667676
db.help(&format!("{} or calling a method that returns `{}`", msg, values.expected));

0 commit comments

Comments
 (0)