From 905bace904ded95ef6996684ede37486313b640f Mon Sep 17 00:00:00 2001 From: francorbacho Date: Thu, 5 Oct 2023 14:36:50 +0200 Subject: [PATCH] Highlight redundant arguments instead of the whole format string --- compiler/rustc_builtin_macros/src/errors.rs | 2 +- compiler/rustc_builtin_macros/src/format.rs | 7 +++--- tests/ui/did_you_mean/issue-105225.rs | 8 +++--- tests/ui/did_you_mean/issue-105225.stderr | 28 ++++++++++----------- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/errors.rs b/compiler/rustc_builtin_macros/src/errors.rs index c2097f0bc47..7a8ec5cfdf3 100644 --- a/compiler/rustc_builtin_macros/src/errors.rs +++ b/compiler/rustc_builtin_macros/src/errors.rs @@ -650,7 +650,7 @@ pub(crate) struct FormatPositionalMismatch { #[diag(builtin_macros_format_redundant_args)] pub(crate) struct FormatRedundantArgs { #[primary_span] - pub(crate) fmt_span: Span, + pub(crate) span: MultiSpan, pub(crate) n: usize, #[note] diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index d7fc48026cb..e08fded01fb 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -619,7 +619,7 @@ fn report_missing_placeholders( if !placeholders.is_empty() { if let Some(mut new_diag) = - report_redundant_format_arguments(ecx, fmt_span, &args, used, placeholders) + report_redundant_format_arguments(ecx, &args, used, placeholders) { diag.cancel(); new_diag.emit(); @@ -718,7 +718,6 @@ macro_rules! check_foreign { /// redundant due to implicit captures (e.g. `format!("{x}", x)`). fn report_redundant_format_arguments<'a>( ecx: &mut ExtCtxt<'a>, - fmt_span: Span, args: &FormatArguments, used: &[bool], placeholders: Vec<(Span, &str)>, @@ -769,9 +768,9 @@ fn report_redundant_format_arguments<'a>( } return Some(ecx.create_err(errors::FormatRedundantArgs { - fmt_span, - note: multispan, n: args_spans.len(), + span: MultiSpan::from(args_spans), + note: multispan, sugg: errors::FormatRedundantArgsSugg { spans: suggestion_spans }, })); } diff --git a/tests/ui/did_you_mean/issue-105225.rs b/tests/ui/did_you_mean/issue-105225.rs index 49070b52dd0..5c0d17f5839 100644 --- a/tests/ui/did_you_mean/issue-105225.rs +++ b/tests/ui/did_you_mean/issue-105225.rs @@ -1,5 +1,7 @@ fn main() { let x = 0; + let y = 0; + println!("{x}", x); //~^ ERROR: redundant argument @@ -9,11 +11,9 @@ fn main() { println!("{} {x}", x, x); //~^ ERROR: redundant argument - let y = 0; println!("{x} {y}", x, y); - //~^ ERROR: redundant argument + //~^ ERROR: redundant arguments - let y = 0; println!("{} {} {x} {y} {}", x, x, x, y, y); - //~^ ERROR: redundant argument + //~^ ERROR: redundant arguments } diff --git a/tests/ui/did_you_mean/issue-105225.stderr b/tests/ui/did_you_mean/issue-105225.stderr index dc18e7268b4..b9b04a0fe2e 100644 --- a/tests/ui/did_you_mean/issue-105225.stderr +++ b/tests/ui/did_you_mean/issue-105225.stderr @@ -1,47 +1,47 @@ error: redundant argument - --> $DIR/issue-105225.rs:3:14 + --> $DIR/issue-105225.rs:5:21 | LL | println!("{x}", x); - | ^^^^^ - help: this can be removed + | ^ help: this can be removed | note: the formatting specifier is referencing the binding already - --> $DIR/issue-105225.rs:3:16 + --> $DIR/issue-105225.rs:5:16 | LL | println!("{x}", x); | ^ error: redundant argument - --> $DIR/issue-105225.rs:6:14 + --> $DIR/issue-105225.rs:8:27 | LL | println!("{x} {}", x, x); - | ^^^^^^^^ - help: this can be removed + | ^ help: this can be removed | note: the formatting specifier is referencing the binding already - --> $DIR/issue-105225.rs:6:16 + --> $DIR/issue-105225.rs:8:16 | LL | println!("{x} {}", x, x); | ^ error: redundant argument - --> $DIR/issue-105225.rs:9:14 + --> $DIR/issue-105225.rs:11:27 | LL | println!("{} {x}", x, x); - | ^^^^^^^^ - help: this can be removed + | ^ help: this can be removed | note: the formatting specifier is referencing the binding already - --> $DIR/issue-105225.rs:9:19 + --> $DIR/issue-105225.rs:11:19 | LL | println!("{} {x}", x, x); | ^ error: redundant arguments - --> $DIR/issue-105225.rs:13:14 + --> $DIR/issue-105225.rs:14:25 | LL | println!("{x} {y}", x, y); - | ^^^^^^^^^ + | ^ ^ | note: the formatting specifiers are referencing the bindings already - --> $DIR/issue-105225.rs:13:16 + --> $DIR/issue-105225.rs:14:16 | LL | println!("{x} {y}", x, y); | ^ ^ @@ -52,10 +52,10 @@ LL + println!("{x} {y}", ); | error: redundant arguments - --> $DIR/issue-105225.rs:17:14 + --> $DIR/issue-105225.rs:17:43 | LL | println!("{} {} {x} {y} {}", x, x, x, y, y); - | ^^^^^^^^^^^^^^^^^^ + | ^ ^ | note: the formatting specifiers are referencing the bindings already --> $DIR/issue-105225.rs:17:26