Skip to content

Commit 2684fa3

Browse files
committed
Highlight redundant arguments instead of the whole format string
1 parent 8dae745 commit 2684fa3

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

compiler/rustc_builtin_macros/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ pub(crate) struct FormatPositionalMismatch {
639639
#[diag(builtin_macros_format_redundant_args)]
640640
pub(crate) struct FormatRedundantArgs {
641641
#[primary_span]
642-
pub(crate) fmt_span: Span,
642+
pub(crate) span: MultiSpan,
643643
pub(crate) n: usize,
644644

645645
#[note]

compiler/rustc_builtin_macros/src/format.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ fn report_missing_placeholders(
610610

611611
if !placeholders.is_empty() {
612612
if let Some(mut new_diag) =
613-
report_redundant_format_arguments(ecx, fmt_span, &args, used, placeholders)
613+
report_redundant_format_arguments(ecx, &args, used, placeholders)
614614
{
615615
diag.cancel();
616616
new_diag.emit();
@@ -709,7 +709,6 @@ fn report_missing_placeholders(
709709
/// redundant due to implicit captures (e.g. `format!("{x}", x)`).
710710
fn report_redundant_format_arguments<'a>(
711711
ecx: &mut ExtCtxt<'a>,
712-
fmt_span: Span,
713712
args: &FormatArguments,
714713
used: &[bool],
715714
placeholders: Vec<(Span, &str)>,
@@ -760,9 +759,9 @@ fn report_redundant_format_arguments<'a>(
760759
}
761760

762761
return Some(ecx.create_err(errors::FormatRedundantArgs {
763-
fmt_span,
764-
note: multispan,
765762
n: args_spans.len(),
763+
span: MultiSpan::from(args_spans),
764+
note: multispan,
766765
sugg: errors::FormatRedundantArgsSugg { spans: suggestion_spans },
767766
}));
768767
}

tests/ui/did_you_mean/issue-105225.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
fn main() {
22
let x = 0;
3+
let y = 0;
4+
35
println!("{x}", x);
46
//~^ ERROR: redundant argument
57

@@ -9,11 +11,9 @@ fn main() {
911
println!("{} {x}", x, x);
1012
//~^ ERROR: redundant argument
1113

12-
let y = 0;
1314
println!("{x} {y}", x, y);
14-
//~^ ERROR: redundant argument
15+
//~^ ERROR: redundant arguments
1516

16-
let y = 0;
1717
println!("{} {} {x} {y} {}", x, x, x, y, y);
18-
//~^ ERROR: redundant argument
18+
//~^ ERROR: redundant arguments
1919
}

tests/ui/did_you_mean/issue-105225.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
error: redundant argument
2-
--> $DIR/issue-105225.rs:3:14
2+
--> $DIR/issue-105225.rs:5:21
33
|
44
LL | println!("{x}", x);
5-
| ^^^^^ - help: this can be removed
5+
| ^ help: this can be removed
66
|
77
note: the formatting specifier is referencing the binding already
8-
--> $DIR/issue-105225.rs:3:16
8+
--> $DIR/issue-105225.rs:5:16
99
|
1010
LL | println!("{x}", x);
1111
| ^
1212

1313
error: redundant argument
14-
--> $DIR/issue-105225.rs:6:14
14+
--> $DIR/issue-105225.rs:8:27
1515
|
1616
LL | println!("{x} {}", x, x);
17-
| ^^^^^^^^ - help: this can be removed
17+
| ^ help: this can be removed
1818
|
1919
note: the formatting specifier is referencing the binding already
20-
--> $DIR/issue-105225.rs:6:16
20+
--> $DIR/issue-105225.rs:8:16
2121
|
2222
LL | println!("{x} {}", x, x);
2323
| ^
2424

2525
error: redundant argument
26-
--> $DIR/issue-105225.rs:9:14
26+
--> $DIR/issue-105225.rs:11:27
2727
|
2828
LL | println!("{} {x}", x, x);
29-
| ^^^^^^^^ - help: this can be removed
29+
| ^ help: this can be removed
3030
|
3131
note: the formatting specifier is referencing the binding already
32-
--> $DIR/issue-105225.rs:9:19
32+
--> $DIR/issue-105225.rs:11:19
3333
|
3434
LL | println!("{} {x}", x, x);
3535
| ^
3636

3737
error: redundant arguments
38-
--> $DIR/issue-105225.rs:13:14
38+
--> $DIR/issue-105225.rs:14:25
3939
|
4040
LL | println!("{x} {y}", x, y);
41-
| ^^^^^^^^^
41+
| ^ ^
4242
|
4343
note: the formatting specifiers are referencing the bindings already
44-
--> $DIR/issue-105225.rs:13:16
44+
--> $DIR/issue-105225.rs:14:16
4545
|
4646
LL | println!("{x} {y}", x, y);
4747
| ^ ^
@@ -52,10 +52,10 @@ LL + println!("{x} {y}", );
5252
|
5353

5454
error: redundant arguments
55-
--> $DIR/issue-105225.rs:17:14
55+
--> $DIR/issue-105225.rs:17:43
5656
|
5757
LL | println!("{} {} {x} {y} {}", x, x, x, y, y);
58-
| ^^^^^^^^^^^^^^^^^^
58+
| ^ ^
5959
|
6060
note: the formatting specifiers are referencing the bindings already
6161
--> $DIR/issue-105225.rs:17:26

0 commit comments

Comments
 (0)