1
Fork 0

review comments

This commit is contained in:
Esteban Küber 2023-11-14 01:31:03 +00:00
parent 4d16171f56
commit a519c9b6b7
2 changed files with 24 additions and 43 deletions

View file

@ -2121,27 +2121,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if !self.can_eq(self.param_env, ret_ty, adt_ty) { if !self.can_eq(self.param_env, ret_ty, adt_ty) {
return None; return None;
} }
// Check for `-> Self`
let input_len = fn_sig.inputs().skip_binder().len(); let input_len = fn_sig.inputs().skip_binder().len();
if def.did() == def_id { let order = !item.name.as_str().starts_with("new");
let order = if item.name.as_str().starts_with("new") { 0 } else { 1 };
Some((order, item.name, input_len)) Some((order, item.name, input_len))
} else {
None
}
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
items.sort_by_key(|(order, _, _)| *order); items.sort_by_key(|(order, _, _)| *order);
let suggestion = |name, args| {
format!(
"::{name}({})",
std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
)
};
match &items[..] { match &items[..] {
[] => {} [] => {}
[(_, name, args)] => { [(_, name, args)] => {
err.span_suggestion_verbose( err.span_suggestion_verbose(
span.shrink_to_hi().with_hi(expr_span.hi()), span.shrink_to_hi().with_hi(expr_span.hi()),
format!("you might have meant to use the `{name}` associated function",), format!("you might have meant to use the `{name}` associated function"),
format!( suggestion(name, *args),
"::{name}({})",
std::iter::repeat("_").take(*args).collect::<Vec<_>>().join(", ")
),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} }
@ -2151,15 +2149,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"you might have meant to use an associated function to build this type", "you might have meant to use an associated function to build this type",
items items
.iter() .iter()
.map(|(_, name, args)| { .map(|(_, name, args)| suggestion(name, *args))
format!(
"::{name}({})",
std::iter::repeat("_")
.take(*args)
.collect::<Vec<_>>()
.join(", ")
)
})
.collect::<Vec<String>>(), .collect::<Vec<String>>(),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );

View file

@ -1732,6 +1732,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
args: &[P<Expr>], args: &[P<Expr>],
) { ) {
if def_id.is_local() { if def_id.is_local() {
// Doing analysis on local `DefId`s would cause infinite recursion.
return; return;
} }
// Look at all the associated functions without receivers in the type's // Look at all the associated functions without receivers in the type's
@ -1752,23 +1753,21 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
let ty::Adt(def, _args) = ret_ty.kind() else { let ty::Adt(def, _args) = ret_ty.kind() else {
return None; return None;
}; };
// Check for `-> Self`
let input_len = fn_sig.inputs().skip_binder().len(); let input_len = fn_sig.inputs().skip_binder().len();
if def.did() == def_id { if def.did() != def_id {
let order = if item.name.as_str().starts_with("new") { return None;
0
} else if input_len == args.len() {
2
} else {
3
};
Some((order, item.name, input_len))
} else {
None
} }
let order = !item.name.as_str().starts_with("new");
Some((order, item.name, input_len))
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
items.sort_by_key(|(order, _, _)| *order); items.sort_by_key(|(order, _, _)| *order);
let suggestion = |name, args| {
format!(
"::{name}({})",
std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
)
};
match &items[..] { match &items[..] {
[] => {} [] => {}
[(_, name, len)] if *len == args.len() => { [(_, name, len)] if *len == args.len() => {
@ -1783,10 +1782,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
err.span_suggestion_verbose( err.span_suggestion_verbose(
path_span.shrink_to_hi().with_hi(call_span.hi()), path_span.shrink_to_hi().with_hi(call_span.hi()),
format!("you might have meant to use the `{name}` associated function",), format!("you might have meant to use the `{name}` associated function",),
format!( suggestion(name, *len),
"::{name}({})",
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} }
@ -1796,12 +1792,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
"you might have meant to use an associated function to build this type", "you might have meant to use an associated function to build this type",
items items
.iter() .iter()
.map(|(_, name, len)| { .map(|(_, name, len)| suggestion(name, *len))
format!(
"::{name}({})",
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
)
})
.collect::<Vec<String>>(), .collect::<Vec<String>>(),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
SuggestionStyle::ShowAlways, SuggestionStyle::ShowAlways,