diff --git a/compiler/rustc_builtin_macros/src/errors.rs b/compiler/rustc_builtin_macros/src/errors.rs index 7a8ec5cfdf3..fde4270334b 100644 --- a/compiler/rustc_builtin_macros/src/errors.rs +++ b/compiler/rustc_builtin_macros/src/errors.rs @@ -657,7 +657,7 @@ pub(crate) struct FormatRedundantArgs { pub(crate) note: MultiSpan, #[subdiagnostic] - pub(crate) sugg: FormatRedundantArgsSugg, + pub(crate) sugg: Option, } #[derive(Subdiagnostic)] diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index e08fded01fb..7c78be17653 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -767,11 +767,17 @@ fn report_redundant_format_arguments<'a>( suggestion_spans.push(span); } + let sugg = if args.named_args().len() == 0 { + Some(errors::FormatRedundantArgsSugg { spans: suggestion_spans }) + } else { + None + }; + return Some(ecx.create_err(errors::FormatRedundantArgs { n: args_spans.len(), span: MultiSpan::from(args_spans), note: multispan, - sugg: errors::FormatRedundantArgsSugg { spans: suggestion_spans }, + sugg, })); } diff --git a/tests/ui/did_you_mean/issue-105225-named-args.rs b/tests/ui/did_you_mean/issue-105225-named-args.rs new file mode 100644 index 00000000000..38e81776576 --- /dev/null +++ b/tests/ui/did_you_mean/issue-105225-named-args.rs @@ -0,0 +1,10 @@ +fn main() { + let x = "x"; + let y = "y"; + + println!("{x}", x, x = y); + //~^ ERROR: redundant argument + + println!("{x}", x = y, x = y); + //~^ ERROR: duplicate argument named `x` +} diff --git a/tests/ui/did_you_mean/issue-105225-named-args.stderr b/tests/ui/did_you_mean/issue-105225-named-args.stderr new file mode 100644 index 00000000000..72204102ef6 --- /dev/null +++ b/tests/ui/did_you_mean/issue-105225-named-args.stderr @@ -0,0 +1,22 @@ +error: redundant argument + --> $DIR/issue-105225-named-args.rs:5:21 + | +LL | println!("{x}", x, x = y); + | ^ + | +note: the formatting specifier is referencing the binding already + --> $DIR/issue-105225-named-args.rs:5:16 + | +LL | println!("{x}", x, x = y); + | ^ + +error: duplicate argument named `x` + --> $DIR/issue-105225-named-args.rs:8:28 + | +LL | println!("{x}", x = y, x = y); + | - ^ duplicate argument + | | + | previously here + +error: aborting due to 2 previous errors + diff --git a/tests/ui/did_you_mean/issue-105225.fixed b/tests/ui/did_you_mean/issue-105225.fixed index 30c99221912..f756be615a1 100644 --- a/tests/ui/did_you_mean/issue-105225.fixed +++ b/tests/ui/did_you_mean/issue-105225.fixed @@ -1,8 +1,8 @@ // run-rustfix fn main() { - let x = 0; - let y = 0; + let x = "x"; + let y = "y"; println!("{x}", ); //~^ ERROR: redundant argument diff --git a/tests/ui/did_you_mean/issue-105225.rs b/tests/ui/did_you_mean/issue-105225.rs index a69c90a6490..91cdf0eb28f 100644 --- a/tests/ui/did_you_mean/issue-105225.rs +++ b/tests/ui/did_you_mean/issue-105225.rs @@ -1,8 +1,8 @@ // run-rustfix fn main() { - let x = 0; - let y = 0; + let x = "x"; + let y = "y"; println!("{x}", x); //~^ ERROR: redundant argument