Skip to content

Commit

Permalink
Only give autofix suggestion when no named args are present
Browse files Browse the repository at this point in the history
  • Loading branch information
francorbacho committed Oct 5, 2023
1 parent 27f29d5 commit 4d235f3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ pub(crate) struct FormatRedundantArgs {
pub(crate) note: MultiSpan,

#[subdiagnostic]
pub(crate) sugg: FormatRedundantArgsSugg,
pub(crate) sugg: Option<FormatRedundantArgsSugg>,
}

#[derive(Subdiagnostic)]
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,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,
}));
}

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/did_you_mean/issue-105225-named-args.rs
Original file line number Diff line number Diff line change
@@ -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`
}
22 changes: 22 additions & 0 deletions tests/ui/did_you_mean/issue-105225-named-args.stderr
Original file line number Diff line number Diff line change
@@ -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

4 changes: 2 additions & 2 deletions tests/ui/did_you_mean/issue-105225.fixed
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/did_you_mean/issue-105225.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4d235f3

Please sign in to comment.