Account for number of arguments in suggestion

This commit is contained in:
Esteban Küber 2023-11-09 01:23:10 +00:00
parent 00265f0cc0
commit 4d16171f56
2 changed files with 39 additions and 20 deletions

View File

@ -1762,16 +1762,16 @@ fn suggest_alternative_construction_methods(
} else {
3
};
Some((order, item.name))
Some((order, item.name, input_len))
} else {
None
}
})
.collect::<Vec<_>>();
items.sort_by_key(|(order, _)| *order);
items.sort_by_key(|(order, _, _)| *order);
match &items[..] {
[] => {}
[(_, name)] => {
[(_, name, len)] if *len == args.len() => {
err.span_suggestion_verbose(
path_span.shrink_to_hi(),
format!("you might have meant to use the `{name}` associated function",),
@ -1779,11 +1779,30 @@ fn suggest_alternative_construction_methods(
Applicability::MaybeIncorrect,
);
}
[(_, name, len)] => {
err.span_suggestion_verbose(
path_span.shrink_to_hi().with_hi(call_span.hi()),
format!("you might have meant to use the `{name}` associated function",),
format!(
"::{name}({})",
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
),
Applicability::MaybeIncorrect,
);
}
_ => {
err.span_suggestions_with_style(
path_span.shrink_to_hi(),
path_span.shrink_to_hi().with_hi(call_span.hi()),
"you might have meant to use an associated function to build this type",
items.iter().map(|(_, name)| format!("::{name}")).collect::<Vec<String>>(),
items
.iter()
.map(|(_, name, len)| {
format!(
"::{name}({})",
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
)
})
.collect::<Vec<String>>(),
Applicability::MaybeIncorrect,
SuggestionStyle::ShowAlways,
);

View File

@ -10,13 +10,13 @@ LL | let _ = std::collections::HashMap();
help: you might have meant to use an associated function to build this type
|
LL | let _ = std::collections::HashMap::new();
| +++++
LL | let _ = std::collections::HashMap::with_capacity();
| +++++++++++++++
LL | let _ = std::collections::HashMap::with_hasher();
| +++++++++++++
LL | let _ = std::collections::HashMap::with_capacity_and_hasher();
| ++++++++++++++++++++++++++
| ~~~~~~~
LL | let _ = std::collections::HashMap::with_capacity(_);
| ~~~~~~~~~~~~~~~~~~
LL | let _ = std::collections::HashMap::with_hasher(_);
| ~~~~~~~~~~~~~~~~
LL | let _ = std::collections::HashMap::with_capacity_and_hasher(_, _);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: consider using the `Default` trait
|
LL | let _ = <std::collections::HashMap as std::default::Default>::default();
@ -36,14 +36,14 @@ note: constructor is not visible here due to private fields
= note: private field
help: you might have meant to use an associated function to build this type
|
LL | wtf: Some(Box::new(U {
| +++++
LL | wtf: Some(Box::new_uninit(U {
| ++++++++++++
LL | wtf: Some(Box::new_zeroed(U {
| ++++++++++++
LL | wtf: Some(Box::new_in(U {
| ++++++++
LL | wtf: Some(Box::new(_)),
| ~~~~~~~~
LL | wtf: Some(Box::new_uninit()),
| ~~~~~~~~~~~~~~
LL | wtf: Some(Box::new_zeroed()),
| ~~~~~~~~~~~~~~
LL | wtf: Some(Box::new_in(_, _)),
| ~~~~~~~~~~~~~~
and 10 other candidates
help: consider using the `Default` trait
|