Rollup merge of #100058 - TaKO8Ki:suggest-positional-formatting-argument-instead-of-format-args-capture, r=estebank
Suggest a positional formatting argument instead of a captured argument This patch fixes a part of #96999. fixes #98241 fixes #97311 r? `@estebank`
This commit is contained in:
commit
d3aa757ff8
5 changed files with 182 additions and 8 deletions
18
src/test/ui/fmt/struct-field-as-captured-argument.fixed
Normal file
18
src/test/ui/fmt/struct-field-as-captured-argument.fixed
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// run-rustfix
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Foo {
|
||||
field: usize,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let foo = Foo { field: 0 };
|
||||
let bar = 3;
|
||||
format!("{0}", foo.field); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{1} {} {bar}", "aa", foo.field); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{2} {} {1} {bar}", "aa", "bb", foo.field); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{1} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{1:?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{1:#?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
|
||||
}
|
||||
18
src/test/ui/fmt/struct-field-as-captured-argument.rs
Normal file
18
src/test/ui/fmt/struct-field-as-captured-argument.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// run-rustfix
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Foo {
|
||||
field: usize,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let foo = Foo { field: 0 };
|
||||
let bar = 3;
|
||||
format!("{foo.field}"); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{foo.field} {} {bar}", "aa"); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{foo.field} {} {1} {bar}", "aa", "bb"); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{foo.field} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{foo.field:?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{foo.field:#?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
|
||||
format!("{foo.field:.3} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
|
||||
}
|
||||
79
src/test/ui/fmt/struct-field-as-captured-argument.stderr
Normal file
79
src/test/ui/fmt/struct-field-as-captured-argument.stderr
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
error: invalid format string: field access isn't supported
|
||||
--> $DIR/struct-field-as-captured-argument.rs:11:15
|
||||
|
|
||||
LL | format!("{foo.field}");
|
||||
| ^^^^^^^^^ not supported in format string
|
||||
|
|
||||
help: consider using a positional formatting argument instead
|
||||
|
|
||||
LL | format!("{0}", foo.field);
|
||||
| ~ +++++++++++
|
||||
|
||||
error: invalid format string: field access isn't supported
|
||||
--> $DIR/struct-field-as-captured-argument.rs:12:15
|
||||
|
|
||||
LL | format!("{foo.field} {} {bar}", "aa");
|
||||
| ^^^^^^^^^ not supported in format string
|
||||
|
|
||||
help: consider using a positional formatting argument instead
|
||||
|
|
||||
LL | format!("{1} {} {bar}", "aa", foo.field);
|
||||
| ~ +++++++++++
|
||||
|
||||
error: invalid format string: field access isn't supported
|
||||
--> $DIR/struct-field-as-captured-argument.rs:13:15
|
||||
|
|
||||
LL | format!("{foo.field} {} {1} {bar}", "aa", "bb");
|
||||
| ^^^^^^^^^ not supported in format string
|
||||
|
|
||||
help: consider using a positional formatting argument instead
|
||||
|
|
||||
LL | format!("{2} {} {1} {bar}", "aa", "bb", foo.field);
|
||||
| ~ +++++++++++
|
||||
|
||||
error: invalid format string: field access isn't supported
|
||||
--> $DIR/struct-field-as-captured-argument.rs:14:15
|
||||
|
|
||||
LL | format!("{foo.field} {} {baz}", "aa", baz = 3);
|
||||
| ^^^^^^^^^ not supported in format string
|
||||
|
|
||||
help: consider using a positional formatting argument instead
|
||||
|
|
||||
LL | format!("{1} {} {baz}", "aa", foo.field, baz = 3);
|
||||
| ~ +++++++++++
|
||||
|
||||
error: invalid format string: field access isn't supported
|
||||
--> $DIR/struct-field-as-captured-argument.rs:15:15
|
||||
|
|
||||
LL | format!("{foo.field:?} {} {baz}", "aa", baz = 3);
|
||||
| ^^^^^^^^^ not supported in format string
|
||||
|
|
||||
help: consider using a positional formatting argument instead
|
||||
|
|
||||
LL | format!("{1:?} {} {baz}", "aa", foo.field, baz = 3);
|
||||
| ~ +++++++++++
|
||||
|
||||
error: invalid format string: field access isn't supported
|
||||
--> $DIR/struct-field-as-captured-argument.rs:16:15
|
||||
|
|
||||
LL | format!("{foo.field:#?} {} {baz}", "aa", baz = 3);
|
||||
| ^^^^^^^^^ not supported in format string
|
||||
|
|
||||
help: consider using a positional formatting argument instead
|
||||
|
|
||||
LL | format!("{1:#?} {} {baz}", "aa", foo.field, baz = 3);
|
||||
| ~ +++++++++++
|
||||
|
||||
error: invalid format string: field access isn't supported
|
||||
--> $DIR/struct-field-as-captured-argument.rs:17:15
|
||||
|
|
||||
LL | format!("{foo.field:.3} {} {baz}", "aa", baz = 3);
|
||||
| ^^^^^^^^^ not supported in format string
|
||||
|
|
||||
help: consider using a positional formatting argument instead
|
||||
|
|
||||
LL | format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3);
|
||||
| ~ +++++++++++
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue