From 3d8c7d2c0a83a7603c3fc4164accfc41a38a5415 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sat, 20 Aug 2022 23:29:38 +0000 Subject: [PATCH] sugg: take into count the debug formatting Signed-off-by: Vincenzo Palazzo --- compiler/rustc_lint/src/context.rs | 9 ++++++-- ...sugg_with_positional_args_and_debug_fmt.rs | 10 +++++++++ ..._with_positional_args_and_debug_fmt.stderr | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/suggestions/sugg_with_positional_args_and_debug_fmt.rs create mode 100644 src/test/ui/suggestions/sugg_with_positional_args_and_debug_fmt.stderr diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index b95fc341db656..6586fe440f362 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -865,9 +865,14 @@ pub trait LintContext: Sized { if let Some(positional_arg_to_replace) = position_sp_to_replace { let name = if is_formatting_arg { named_arg_name + "$" } else { named_arg_name }; - + let span_to_replace = if let Ok(positional_arg_content) = + self.sess().source_map().span_to_snippet(positional_arg_to_replace) && positional_arg_content.starts_with(":") { + positional_arg_to_replace.shrink_to_lo() + } else { + positional_arg_to_replace + }; db.span_suggestion_verbose( - positional_arg_to_replace, + span_to_replace, "use the named argument by name to avoid ambiguity", name, Applicability::MaybeIncorrect, diff --git a/src/test/ui/suggestions/sugg_with_positional_args_and_debug_fmt.rs b/src/test/ui/suggestions/sugg_with_positional_args_and_debug_fmt.rs new file mode 100644 index 0000000000000..21ab6830b3c81 --- /dev/null +++ b/src/test/ui/suggestions/sugg_with_positional_args_and_debug_fmt.rs @@ -0,0 +1,10 @@ +// When build the suggesttion take in consideration the `:?` +// https://github.com/rust-lang/rust/issues/100648 +#![deny(warnings)] + +fn main () { + println!("hello {:?}", world = "world"); + //~^ ERROR named argument `world` is not used by name + //~| HELP use the named argument by name to avoid ambiguity + //~| SUGGESTION world +} diff --git a/src/test/ui/suggestions/sugg_with_positional_args_and_debug_fmt.stderr b/src/test/ui/suggestions/sugg_with_positional_args_and_debug_fmt.stderr new file mode 100644 index 0000000000000..850f69f2d9853 --- /dev/null +++ b/src/test/ui/suggestions/sugg_with_positional_args_and_debug_fmt.stderr @@ -0,0 +1,21 @@ +error: named argument `world` is not used by name + --> $DIR/sugg_with_positional_args_and_debug_fmt.rs:6:28 + | +LL | println!("hello {:?}", world = "world"); + | ---- ^^^^^ this named argument is referred to by position in formatting string + | | + | this formatting argument uses named argument `world` by position + | +note: the lint level is defined here + --> $DIR/sugg_with_positional_args_and_debug_fmt.rs:3:9 + | +LL | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(named_arguments_used_positionally)]` implied by `#[deny(warnings)]` +help: use the named argument by name to avoid ambiguity + | +LL | println!("hello {world:?}", world = "world"); + | +++++ + +error: aborting due to previous error +