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:
Matthias Krüger 2022-08-04 22:25:01 +02:00 committed by GitHub
commit d3aa757ff8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 182 additions and 8 deletions

View 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
}

View 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
}

View 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