Skip to content

Commit 0d5223d

Browse files
authored
Rollup merge of rust-lang#100826 - vincenzopalazzo:macros/wrong_sugg_with_positional_arg, r=TaKO8Ki
sugg: take into count the debug formatting Closes rust-lang#100648 This PR will fix a suggestion error by taking into consideration also the `:?` symbol and act in a different way `@rustbot` r? `@compiler-errors` N.B: I did not find a full way to test the change, any idea?
2 parents 085ba81 + 3d8c7d2 commit 0d5223d

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

compiler/rustc_lint/src/context.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -865,9 +865,14 @@ pub trait LintContext: Sized {
865865

866866
if let Some(positional_arg_to_replace) = position_sp_to_replace {
867867
let name = if is_formatting_arg { named_arg_name + "$" } else { named_arg_name };
868-
868+
let span_to_replace = if let Ok(positional_arg_content) =
869+
self.sess().source_map().span_to_snippet(positional_arg_to_replace) && positional_arg_content.starts_with(":") {
870+
positional_arg_to_replace.shrink_to_lo()
871+
} else {
872+
positional_arg_to_replace
873+
};
869874
db.span_suggestion_verbose(
870-
positional_arg_to_replace,
875+
span_to_replace,
871876
"use the named argument by name to avoid ambiguity",
872877
name,
873878
Applicability::MaybeIncorrect,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// When build the suggesttion take in consideration the `:?`
2+
// https://github.com/rust-lang/rust/issues/100648
3+
#![deny(warnings)]
4+
5+
fn main () {
6+
println!("hello {:?}", world = "world");
7+
//~^ ERROR named argument `world` is not used by name
8+
//~| HELP use the named argument by name to avoid ambiguity
9+
//~| SUGGESTION world
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: named argument `world` is not used by name
2+
--> $DIR/sugg_with_positional_args_and_debug_fmt.rs:6:28
3+
|
4+
LL | println!("hello {:?}", world = "world");
5+
| ---- ^^^^^ this named argument is referred to by position in formatting string
6+
| |
7+
| this formatting argument uses named argument `world` by position
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/sugg_with_positional_args_and_debug_fmt.rs:3:9
11+
|
12+
LL | #![deny(warnings)]
13+
| ^^^^^^^^
14+
= note: `#[deny(named_arguments_used_positionally)]` implied by `#[deny(warnings)]`
15+
help: use the named argument by name to avoid ambiguity
16+
|
17+
LL | println!("hello {world:?}", world = "world");
18+
| +++++
19+
20+
error: aborting due to previous error
21+

0 commit comments

Comments
 (0)