Skip to content

Commit 4d235f3

Browse files
committed
Only give autofix suggestion when no named args are present
1 parent 27f29d5 commit 4d235f3

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

compiler/rustc_builtin_macros/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ pub(crate) struct FormatRedundantArgs {
646646
pub(crate) note: MultiSpan,
647647

648648
#[subdiagnostic]
649-
pub(crate) sugg: FormatRedundantArgsSugg,
649+
pub(crate) sugg: Option<FormatRedundantArgsSugg>,
650650
}
651651

652652
#[derive(Subdiagnostic)]

compiler/rustc_builtin_macros/src/format.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,17 @@ fn report_redundant_format_arguments<'a>(
758758
suggestion_spans.push(span);
759759
}
760760

761+
let sugg = if args.named_args().len() == 0 {
762+
Some(errors::FormatRedundantArgsSugg { spans: suggestion_spans })
763+
} else {
764+
None
765+
};
766+
761767
return Some(ecx.create_err(errors::FormatRedundantArgs {
762768
n: args_spans.len(),
763769
span: MultiSpan::from(args_spans),
764770
note: multispan,
765-
sugg: errors::FormatRedundantArgsSugg { spans: suggestion_spans },
771+
sugg,
766772
}));
767773
}
768774

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
let x = "x";
3+
let y = "y";
4+
5+
println!("{x}", x, x = y);
6+
//~^ ERROR: redundant argument
7+
8+
println!("{x}", x = y, x = y);
9+
//~^ ERROR: duplicate argument named `x`
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: redundant argument
2+
--> $DIR/issue-105225-named-args.rs:5:21
3+
|
4+
LL | println!("{x}", x, x = y);
5+
| ^
6+
|
7+
note: the formatting specifier is referencing the binding already
8+
--> $DIR/issue-105225-named-args.rs:5:16
9+
|
10+
LL | println!("{x}", x, x = y);
11+
| ^
12+
13+
error: duplicate argument named `x`
14+
--> $DIR/issue-105225-named-args.rs:8:28
15+
|
16+
LL | println!("{x}", x = y, x = y);
17+
| - ^ duplicate argument
18+
| |
19+
| previously here
20+
21+
error: aborting due to 2 previous errors
22+

tests/ui/did_you_mean/issue-105225.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// run-rustfix
22

33
fn main() {
4-
let x = 0;
5-
let y = 0;
4+
let x = "x";
5+
let y = "y";
66

77
println!("{x}", );
88
//~^ ERROR: redundant argument

tests/ui/did_you_mean/issue-105225.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// run-rustfix
22

33
fn main() {
4-
let x = 0;
5-
let y = 0;
4+
let x = "x";
5+
let y = "y";
66

77
println!("{x}", x);
88
//~^ ERROR: redundant argument

0 commit comments

Comments
 (0)