Skip to content

Commit 75b0f08

Browse files
committed
review comments
1 parent 491757b commit 75b0f08

File tree

2 files changed

+24
-43
lines changed

2 files changed

+24
-43
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -2121,27 +2121,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21212121
if !self.can_eq(self.param_env, ret_ty, adt_ty) {
21222122
return None;
21232123
}
2124-
// Check for `-> Self`
21252124
let input_len = fn_sig.inputs().skip_binder().len();
2126-
if def.did() == def_id {
2127-
let order = if item.name.as_str().starts_with("new") { 0 } else { 1 };
2128-
Some((order, item.name, input_len))
2129-
} else {
2130-
None
2131-
}
2125+
let order = !item.name.as_str().starts_with("new");
2126+
Some((order, item.name, input_len))
21322127
})
21332128
.collect::<Vec<_>>();
21342129
items.sort_by_key(|(order, _, _)| *order);
2130+
let suggestion = |name, args| {
2131+
format!(
2132+
"::{name}({})",
2133+
std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
2134+
)
2135+
};
21352136
match &items[..] {
21362137
[] => {}
21372138
[(_, name, args)] => {
21382139
err.span_suggestion_verbose(
21392140
span.shrink_to_hi().with_hi(expr_span.hi()),
2140-
format!("you might have meant to use the `{name}` associated function",),
2141-
format!(
2142-
"::{name}({})",
2143-
std::iter::repeat("_").take(*args).collect::<Vec<_>>().join(", ")
2144-
),
2141+
format!("you might have meant to use the `{name}` associated function"),
2142+
suggestion(name, *args),
21452143
Applicability::MaybeIncorrect,
21462144
);
21472145
}
@@ -2151,15 +2149,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21512149
"you might have meant to use an associated function to build this type",
21522150
items
21532151
.iter()
2154-
.map(|(_, name, args)| {
2155-
format!(
2156-
"::{name}({})",
2157-
std::iter::repeat("_")
2158-
.take(*args)
2159-
.collect::<Vec<_>>()
2160-
.join(", ")
2161-
)
2162-
})
2152+
.map(|(_, name, args)| suggestion(name, *args))
21632153
.collect::<Vec<String>>(),
21642154
Applicability::MaybeIncorrect,
21652155
);

compiler/rustc_resolve/src/late/diagnostics.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17051705
args: &[P<Expr>],
17061706
) {
17071707
if def_id.is_local() {
1708+
// Doing analysis on local `DefId`s would cause infinite recursion.
17081709
return;
17091710
}
17101711
// Look at all the associated functions without receivers in the type's
@@ -1725,23 +1726,21 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17251726
let ty::Adt(def, _args) = ret_ty.kind() else {
17261727
return None;
17271728
};
1728-
// Check for `-> Self`
17291729
let input_len = fn_sig.inputs().skip_binder().len();
1730-
if def.did() == def_id {
1731-
let order = if item.name.as_str().starts_with("new") {
1732-
0
1733-
} else if input_len == args.len() {
1734-
2
1735-
} else {
1736-
3
1737-
};
1738-
Some((order, item.name, input_len))
1739-
} else {
1740-
None
1730+
if def.did() != def_id {
1731+
return None;
17411732
}
1733+
let order = !item.name.as_str().starts_with("new");
1734+
Some((order, item.name, input_len))
17421735
})
17431736
.collect::<Vec<_>>();
17441737
items.sort_by_key(|(order, _, _)| *order);
1738+
let suggestion = |name, args| {
1739+
format!(
1740+
"::{name}({})",
1741+
std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
1742+
)
1743+
};
17451744
match &items[..] {
17461745
[] => {}
17471746
[(_, name, len)] if *len == args.len() => {
@@ -1756,10 +1755,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17561755
err.span_suggestion_verbose(
17571756
path_span.shrink_to_hi().with_hi(call_span.hi()),
17581757
format!("you might have meant to use the `{name}` associated function",),
1759-
format!(
1760-
"::{name}({})",
1761-
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
1762-
),
1758+
suggestion(name, *len),
17631759
Applicability::MaybeIncorrect,
17641760
);
17651761
}
@@ -1769,12 +1765,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17691765
"you might have meant to use an associated function to build this type",
17701766
items
17711767
.iter()
1772-
.map(|(_, name, len)| {
1773-
format!(
1774-
"::{name}({})",
1775-
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
1776-
)
1777-
})
1768+
.map(|(_, name, len)| suggestion(name, *len))
17781769
.collect::<Vec<String>>(),
17791770
Applicability::MaybeIncorrect,
17801771
SuggestionStyle::ShowAlways,

0 commit comments

Comments
 (0)