Skip to content

Commit 491757b

Browse files
committed
Account for number of arguments in suggestion
1 parent 089fdeb commit 491757b

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,28 +1735,47 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17351735
} else {
17361736
3
17371737
};
1738-
Some((order, item.name))
1738+
Some((order, item.name, input_len))
17391739
} else {
17401740
None
17411741
}
17421742
})
17431743
.collect::<Vec<_>>();
1744-
items.sort_by_key(|(order, _)| *order);
1744+
items.sort_by_key(|(order, _, _)| *order);
17451745
match &items[..] {
17461746
[] => {}
1747-
[(_, name)] => {
1747+
[(_, name, len)] if *len == args.len() => {
17481748
err.span_suggestion_verbose(
17491749
path_span.shrink_to_hi(),
17501750
format!("you might have meant to use the `{name}` associated function",),
17511751
format!("::{name}"),
17521752
Applicability::MaybeIncorrect,
17531753
);
17541754
}
1755+
[(_, name, len)] => {
1756+
err.span_suggestion_verbose(
1757+
path_span.shrink_to_hi().with_hi(call_span.hi()),
1758+
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+
),
1763+
Applicability::MaybeIncorrect,
1764+
);
1765+
}
17551766
_ => {
17561767
err.span_suggestions_with_style(
1757-
path_span.shrink_to_hi(),
1768+
path_span.shrink_to_hi().with_hi(call_span.hi()),
17581769
"you might have meant to use an associated function to build this type",
1759-
items.iter().map(|(_, name)| format!("::{name}")).collect::<Vec<String>>(),
1770+
items
1771+
.iter()
1772+
.map(|(_, name, len)| {
1773+
format!(
1774+
"::{name}({})",
1775+
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
1776+
)
1777+
})
1778+
.collect::<Vec<String>>(),
17601779
Applicability::MaybeIncorrect,
17611780
SuggestionStyle::ShowAlways,
17621781
);

tests/ui/privacy/suggest-box-new.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ LL | let _ = std::collections::HashMap();
1010
help: you might have meant to use an associated function to build this type
1111
|
1212
LL | let _ = std::collections::HashMap::new();
13-
| +++++
14-
LL | let _ = std::collections::HashMap::with_capacity();
15-
| +++++++++++++++
16-
LL | let _ = std::collections::HashMap::with_hasher();
17-
| +++++++++++++
18-
LL | let _ = std::collections::HashMap::with_capacity_and_hasher();
19-
| ++++++++++++++++++++++++++
13+
| ~~~~~~~
14+
LL | let _ = std::collections::HashMap::with_capacity(_);
15+
| ~~~~~~~~~~~~~~~~~~
16+
LL | let _ = std::collections::HashMap::with_hasher(_);
17+
| ~~~~~~~~~~~~~~~~
18+
LL | let _ = std::collections::HashMap::with_capacity_and_hasher(_, _);
19+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2020
help: consider using the `Default` trait
2121
|
2222
LL | let _ = <std::collections::HashMap as std::default::Default>::default();
@@ -36,14 +36,14 @@ note: constructor is not visible here due to private fields
3636
= note: private field
3737
help: you might have meant to use an associated function to build this type
3838
|
39-
LL | wtf: Some(Box::new(U {
40-
| +++++
41-
LL | wtf: Some(Box::new_uninit(U {
42-
| ++++++++++++
43-
LL | wtf: Some(Box::new_zeroed(U {
44-
| ++++++++++++
45-
LL | wtf: Some(Box::new_in(U {
46-
| ++++++++
39+
LL | wtf: Some(Box::new(_)),
40+
| ~~~~~~~~
41+
LL | wtf: Some(Box::new_uninit()),
42+
| ~~~~~~~~~~~~~~
43+
LL | wtf: Some(Box::new_zeroed()),
44+
| ~~~~~~~~~~~~~~
45+
LL | wtf: Some(Box::new_in(_, _)),
46+
| ~~~~~~~~~~~~~~
4747
and 10 other candidates
4848
help: consider using the `Default` trait
4949
|

0 commit comments

Comments
 (0)