Highlight redundant arguments instead of the whole format string

This commit is contained in:
francorbacho 2023-10-05 14:36:50 +02:00
parent fcdd5c0b2d
commit 905bace904
4 changed files with 22 additions and 23 deletions

View File

@ -650,7 +650,7 @@ pub(crate) struct FormatPositionalMismatch {
#[diag(builtin_macros_format_redundant_args)] #[diag(builtin_macros_format_redundant_args)]
pub(crate) struct FormatRedundantArgs { pub(crate) struct FormatRedundantArgs {
#[primary_span] #[primary_span]
pub(crate) fmt_span: Span, pub(crate) span: MultiSpan,
pub(crate) n: usize, pub(crate) n: usize,
#[note] #[note]

View File

@ -619,7 +619,7 @@ fn report_missing_placeholders(
if !placeholders.is_empty() { if !placeholders.is_empty() {
if let Some(mut new_diag) = 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(); diag.cancel();
new_diag.emit(); new_diag.emit();
@ -718,7 +718,6 @@ fn report_missing_placeholders(
/// redundant due to implicit captures (e.g. `format!("{x}", x)`). /// redundant due to implicit captures (e.g. `format!("{x}", x)`).
fn report_redundant_format_arguments<'a>( fn report_redundant_format_arguments<'a>(
ecx: &mut ExtCtxt<'a>, ecx: &mut ExtCtxt<'a>,
fmt_span: Span,
args: &FormatArguments, args: &FormatArguments,
used: &[bool], used: &[bool],
placeholders: Vec<(Span, &str)>, placeholders: Vec<(Span, &str)>,
@ -769,9 +768,9 @@ fn report_redundant_format_arguments<'a>(
} }
return Some(ecx.create_err(errors::FormatRedundantArgs { return Some(ecx.create_err(errors::FormatRedundantArgs {
fmt_span,
note: multispan,
n: args_spans.len(), n: args_spans.len(),
span: MultiSpan::from(args_spans),
note: multispan,
sugg: errors::FormatRedundantArgsSugg { spans: suggestion_spans }, sugg: errors::FormatRedundantArgsSugg { spans: suggestion_spans },
})); }));
} }

View File

@ -1,5 +1,7 @@
fn main() { fn main() {
let x = 0; let x = 0;
let y = 0;
println!("{x}", x); println!("{x}", x);
//~^ ERROR: redundant argument //~^ ERROR: redundant argument
@ -9,11 +11,9 @@ fn main() {
println!("{} {x}", x, x); println!("{} {x}", x, x);
//~^ ERROR: redundant argument //~^ ERROR: redundant argument
let y = 0;
println!("{x} {y}", x, y); println!("{x} {y}", x, y);
//~^ ERROR: redundant argument //~^ ERROR: redundant arguments
let y = 0;
println!("{} {} {x} {y} {}", x, x, x, y, y); println!("{} {} {x} {y} {}", x, x, x, y, y);
//~^ ERROR: redundant argument //~^ ERROR: redundant arguments
} }

View File

@ -1,47 +1,47 @@
error: redundant argument error: redundant argument
--> $DIR/issue-105225.rs:3:14 --> $DIR/issue-105225.rs:5:21
| |
LL | println!("{x}", x); LL | println!("{x}", x);
| ^^^^^ - help: this can be removed | ^ help: this can be removed
| |
note: the formatting specifier is referencing the binding already 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); LL | println!("{x}", x);
| ^ | ^
error: redundant argument error: redundant argument
--> $DIR/issue-105225.rs:6:14 --> $DIR/issue-105225.rs:8:27
| |
LL | println!("{x} {}", x, x); LL | println!("{x} {}", x, x);
| ^^^^^^^^ - help: this can be removed | ^ help: this can be removed
| |
note: the formatting specifier is referencing the binding already 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); LL | println!("{x} {}", x, x);
| ^ | ^
error: redundant argument error: redundant argument
--> $DIR/issue-105225.rs:9:14 --> $DIR/issue-105225.rs:11:27
| |
LL | println!("{} {x}", x, x); LL | println!("{} {x}", x, x);
| ^^^^^^^^ - help: this can be removed | ^ help: this can be removed
| |
note: the formatting specifier is referencing the binding already 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); LL | println!("{} {x}", x, x);
| ^ | ^
error: redundant arguments error: redundant arguments
--> $DIR/issue-105225.rs:13:14 --> $DIR/issue-105225.rs:14:25
| |
LL | println!("{x} {y}", x, y); LL | println!("{x} {y}", x, y);
| ^^^^^^^^^ | ^ ^
| |
note: the formatting specifiers are referencing the bindings already 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); LL | println!("{x} {y}", x, y);
| ^ ^ | ^ ^
@ -52,10 +52,10 @@ LL + println!("{x} {y}", );
| |
error: redundant arguments error: redundant arguments
--> $DIR/issue-105225.rs:17:14 --> $DIR/issue-105225.rs:17:43
| |
LL | println!("{} {} {x} {y} {}", x, x, x, y, y); LL | println!("{} {} {x} {y} {}", x, x, x, y, y);
| ^^^^^^^^^^^^^^^^^^ | ^ ^
| |
note: the formatting specifiers are referencing the bindings already note: the formatting specifiers are referencing the bindings already
--> $DIR/issue-105225.rs:17:26 --> $DIR/issue-105225.rs:17:26