Suggest local variables for captured format args on note

This commit is contained in:
Yuki Okushi 2026-02-04 20:26:54 +09:00
parent 8387095803
commit 8ea3542a9a
7 changed files with 45 additions and 12 deletions

View file

@ -934,7 +934,11 @@ impl<'input> Parser<'input> {
0,
ParseError {
description: "field access isn't supported".to_string(),
note: None,
note: Some(
"consider moving this expression to a local variable and then \
using the local here instead"
.to_owned(),
),
label: "not supported".to_string(),
span: arg.position_span.start..field.position_span.end,
secondary_label: None,
@ -947,7 +951,11 @@ impl<'input> Parser<'input> {
0,
ParseError {
description: "tuple index access isn't supported".to_string(),
note: None,
note: Some(
"consider moving this expression to a local variable and then \
using the local here instead"
.to_owned(),
),
label: "not supported".to_string(),
span: arg.position_span.start..field.position_span.end,
secondary_label: None,

View file

@ -1,6 +1,7 @@
// Checks that there is a suggestion for simple tuple index access expression (used where an
// identifier is expected in a format arg) to use positional arg instead.
// Issue: <https://github.com/rust-lang/rust/issues/122535>.
// identifier is expected in a format arg) to use positional arg instead, with a note to move
// the expression into a local variable.
// Issue: <https://github.com/rust-lang/rust/issues/114108>.
//@ run-rustfix
fn main() {

View file

@ -1,6 +1,7 @@
// Checks that there is a suggestion for simple tuple index access expression (used where an
// identifier is expected in a format arg) to use positional arg instead.
// Issue: <https://github.com/rust-lang/rust/issues/122535>.
// identifier is expected in a format arg) to use positional arg instead, with a note to move
// the expression into a local variable.
// Issue: <https://github.com/rust-lang/rust/issues/114108>.
//@ run-rustfix
fn main() {

View file

@ -1,9 +1,10 @@
error: invalid format string: tuple index access isn't supported
--> $DIR/format-args-non-identifier-diagnostics.rs:8:16
--> $DIR/format-args-non-identifier-diagnostics.rs:9:16
|
LL | println!("{x.0}");
| ^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - println!("{x.0}");

View file

@ -10,6 +10,7 @@ fn main() {
let bar = 3;
let _ = format!("{0}", foo.field); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1} {} {bar}", "aa", foo.field); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{0:value$} {bar}", foo.field, value = 1); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{2} {} {1} {bar}", "aa", "bb", foo.field); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1:?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported

View file

@ -10,6 +10,7 @@ fn main() {
let bar = 3;
let _ = format!("{foo.field}"); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field} {} {bar}", "aa"); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field:value$} {bar}", value = 1); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field} {} {1} {bar}", "aa", "bb"); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field:?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported

View file

@ -4,6 +4,7 @@ error: invalid format string: field access isn't supported
LL | let _ = format!("{foo.field}");
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field}");
@ -16,6 +17,7 @@ error: invalid format string: field access isn't supported
LL | let _ = format!("{foo.field} {} {bar}", "aa");
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field} {} {bar}", "aa");
@ -25,9 +27,23 @@ LL + let _ = format!("{1} {} {bar}", "aa", foo.field);
error: invalid format string: field access isn't supported
--> $DIR/struct-field-as-captured-argument.rs:13:23
|
LL | let _ = format!("{foo.field:value$} {bar}", value = 1);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field:value$} {bar}", value = 1);
LL + let _ = format!("{0:value$} {bar}", foo.field, value = 1);
|
error: invalid format string: field access isn't supported
--> $DIR/struct-field-as-captured-argument.rs:14:23
|
LL | let _ = format!("{foo.field} {} {1} {bar}", "aa", "bb");
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field} {} {1} {bar}", "aa", "bb");
@ -35,11 +51,12 @@ LL + let _ = 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:23
--> $DIR/struct-field-as-captured-argument.rs:15:23
|
LL | let _ = format!("{foo.field} {} {baz}", "aa", baz = 3);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field} {} {baz}", "aa", baz = 3);
@ -47,11 +64,12 @@ LL + let _ = 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:23
--> $DIR/struct-field-as-captured-argument.rs:16:23
|
LL | let _ = format!("{foo.field:?} {} {baz}", "aa", baz = 3);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field:?} {} {baz}", "aa", baz = 3);
@ -59,11 +77,12 @@ LL + let _ = 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:23
--> $DIR/struct-field-as-captured-argument.rs:17:23
|
LL | let _ = format!("{foo.field:#?} {} {baz}", "aa", baz = 3);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field:#?} {} {baz}", "aa", baz = 3);
@ -71,16 +90,17 @@ LL + let _ = 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:23
--> $DIR/struct-field-as-captured-argument.rs:18:23
|
LL | let _ = format!("{foo.field:.3} {} {baz}", "aa", baz = 3);
| ^^^^^^^^^ not supported in format string
|
= note: consider moving this expression to a local variable and then using the local here instead
help: consider using a positional formatting argument instead
|
LL - let _ = format!("{foo.field:.3} {} {baz}", "aa", baz = 3);
LL + let _ = format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3);
|
error: aborting due to 7 previous errors
error: aborting due to 8 previous errors