Skip to content

Commit 4d16171

Browse files
committed
Account for number of arguments in suggestion
1 parent 00265f0 commit 4d16171

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -1762,28 +1762,47 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17621762
} else {
17631763
3
17641764
};
1765-
Some((order, item.name))
1765+
Some((order, item.name, input_len))
17661766
} else {
17671767
None
17681768
}
17691769
})
17701770
.collect::<Vec<_>>();
1771-
items.sort_by_key(|(order, _)| *order);
1771+
items.sort_by_key(|(order, _, _)| *order);
17721772
match &items[..] {
17731773
[] => {}
1774-
[(_, name)] => {
1774+
[(_, name, len)] if *len == args.len() => {
17751775
err.span_suggestion_verbose(
17761776
path_span.shrink_to_hi(),
17771777
format!("you might have meant to use the `{name}` associated function",),
17781778
format!("::{name}"),
17791779
Applicability::MaybeIncorrect,
17801780
);
17811781
}
1782+
[(_, name, len)] => {
1783+
err.span_suggestion_verbose(
1784+
path_span.shrink_to_hi().with_hi(call_span.hi()),
1785+
format!("you might have meant to use the `{name}` associated function",),
1786+
format!(
1787+
"::{name}({})",
1788+
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
1789+
),
1790+
Applicability::MaybeIncorrect,
1791+
);
1792+
}
17821793
_ => {
17831794
err.span_suggestions_with_style(
1784-
path_span.shrink_to_hi(),
1795+
path_span.shrink_to_hi().with_hi(call_span.hi()),
17851796
"you might have meant to use an associated function to build this type",
1786-
items.iter().map(|(_, name)| format!("::{name}")).collect::<Vec<String>>(),
1797+
items
1798+
.iter()
1799+
.map(|(_, name, len)| {
1800+
format!(
1801+
"::{name}({})",
1802+
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
1803+
)
1804+
})
1805+
.collect::<Vec<String>>(),
17871806
Applicability::MaybeIncorrect,
17881807
SuggestionStyle::ShowAlways,
17891808
);

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

+15-15
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)