From 4233a13cebe176139d8d16d18f507b90852ddab0 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 2 Aug 2022 20:33:40 +0900 Subject: suggest a positional formatting argument instead of a captured argument --- .../ui/fmt/struct-field-as-captured-argument.fixed | 15 +++++++ .../ui/fmt/struct-field-as-captured-argument.rs | 15 +++++++ .../fmt/struct-field-as-captured-argument.stderr | 46 ++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/test/ui/fmt/struct-field-as-captured-argument.fixed create mode 100644 src/test/ui/fmt/struct-field-as-captured-argument.rs create mode 100644 src/test/ui/fmt/struct-field-as-captured-argument.stderr (limited to 'src/test') diff --git a/src/test/ui/fmt/struct-field-as-captured-argument.fixed b/src/test/ui/fmt/struct-field-as-captured-argument.fixed new file mode 100644 index 00000000000..a8d7b44fb3d --- /dev/null +++ b/src/test/ui/fmt/struct-field-as-captured-argument.fixed @@ -0,0 +1,15 @@ +// 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 +} diff --git a/src/test/ui/fmt/struct-field-as-captured-argument.rs b/src/test/ui/fmt/struct-field-as-captured-argument.rs new file mode 100644 index 00000000000..e23c14190b0 --- /dev/null +++ b/src/test/ui/fmt/struct-field-as-captured-argument.rs @@ -0,0 +1,15 @@ +// 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 +} diff --git a/src/test/ui/fmt/struct-field-as-captured-argument.stderr b/src/test/ui/fmt/struct-field-as-captured-argument.stderr new file mode 100644 index 00000000000..28d3c8f838b --- /dev/null +++ b/src/test/ui/fmt/struct-field-as-captured-argument.stderr @@ -0,0 +1,46 @@ +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: aborting due to 4 previous errors + -- cgit 1.4.1-3-g733a5 From a0a2ec332621d35b92633dc4db0451e0c3cb4ab2 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 3 Aug 2022 11:28:00 +0900 Subject: add tests for `Debug` formatters and precision formatters --- .../ui/fmt/struct-field-as-captured-argument.fixed | 3 ++ .../ui/fmt/struct-field-as-captured-argument.rs | 3 ++ .../fmt/struct-field-as-captured-argument.stderr | 35 +++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) (limited to 'src/test') diff --git a/src/test/ui/fmt/struct-field-as-captured-argument.fixed b/src/test/ui/fmt/struct-field-as-captured-argument.fixed index a8d7b44fb3d..f7244f6744f 100644 --- a/src/test/ui/fmt/struct-field-as-captured-argument.fixed +++ b/src/test/ui/fmt/struct-field-as-captured-argument.fixed @@ -12,4 +12,7 @@ fn main() { 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 } diff --git a/src/test/ui/fmt/struct-field-as-captured-argument.rs b/src/test/ui/fmt/struct-field-as-captured-argument.rs index e23c14190b0..ab5f2552bd3 100644 --- a/src/test/ui/fmt/struct-field-as-captured-argument.rs +++ b/src/test/ui/fmt/struct-field-as-captured-argument.rs @@ -12,4 +12,7 @@ fn main() { 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 } diff --git a/src/test/ui/fmt/struct-field-as-captured-argument.stderr b/src/test/ui/fmt/struct-field-as-captured-argument.stderr index 28d3c8f838b..7ea8b4068f2 100644 --- a/src/test/ui/fmt/struct-field-as-captured-argument.stderr +++ b/src/test/ui/fmt/struct-field-as-captured-argument.stderr @@ -42,5 +42,38 @@ help: consider using a positional formatting argument instead LL | format!("{1} {} {baz}", "aa", foo.field, baz = 3); | ~ +++++++++++ -error: aborting due to 4 previous errors +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 -- cgit 1.4.1-3-g733a5