From 4d16171f563761e19798c220ee415f84d62df26c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 9 Nov 2023 01:23:10 +0000 Subject: [PATCH] Account for number of arguments in suggestion --- .../rustc_resolve/src/late/diagnostics.rs | 29 ++++++++++++++---- tests/ui/privacy/suggest-box-new.stderr | 30 +++++++++---------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index a0b0abd6555..b0bb9a383db 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1762,16 +1762,16 @@ fn suggest_alternative_construction_methods( } else { 3 }; - Some((order, item.name)) + Some((order, item.name, input_len)) } else { None } }) .collect::>(); - 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::>().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::>(), + items + .iter() + .map(|(_, name, len)| { + format!( + "::{name}({})", + std::iter::repeat("_").take(*len).collect::>().join(", ") + ) + }) + .collect::>(), Applicability::MaybeIncorrect, SuggestionStyle::ShowAlways, ); diff --git a/tests/ui/privacy/suggest-box-new.stderr b/tests/ui/privacy/suggest-box-new.stderr index 5fec35dc5c6..2224f1c60d6 100644 --- a/tests/ui/privacy/suggest-box-new.stderr +++ b/tests/ui/privacy/suggest-box-new.stderr @@ -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 _ = ::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 |