mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 15:23:46 +00:00
Rollup merge of #122556 - jieyouxu:non-identifier-format-arg, r=petrochenkov
Extend format arg help for simple tuple index access expression The help is only applicable for simple field access `a.b` and (with this PR) simple tuple index access expressions `a.0`. Closes #122535.
This commit is contained in:
commit
45e005df42
@ -907,25 +907,47 @@ impl<'a> Parser<'a> {
|
|||||||
let byte_pos = self.to_span_index(end);
|
let byte_pos = self.to_span_index(end);
|
||||||
let start = InnerOffset(byte_pos.0 + 1);
|
let start = InnerOffset(byte_pos.0 + 1);
|
||||||
let field = self.argument(start);
|
let field = self.argument(start);
|
||||||
// We can only parse `foo.bar` field access, any deeper nesting,
|
// We can only parse simple `foo.bar` field access or `foo.0` tuple index access, any
|
||||||
// or another type of expression, like method calls, are not supported
|
// deeper nesting, or another type of expression, like method calls, are not supported
|
||||||
if !self.consume('}') {
|
if !self.consume('}') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let ArgumentNamed(_) = arg.position {
|
if let ArgumentNamed(_) = arg.position {
|
||||||
if let ArgumentNamed(_) = field.position {
|
match field.position {
|
||||||
self.errors.insert(
|
ArgumentNamed(_) => {
|
||||||
0,
|
self.errors.insert(
|
||||||
ParseError {
|
0,
|
||||||
description: "field access isn't supported".to_string(),
|
ParseError {
|
||||||
note: None,
|
description: "field access isn't supported".to_string(),
|
||||||
label: "not supported".to_string(),
|
note: None,
|
||||||
span: InnerSpan::new(arg.position_span.start, field.position_span.end),
|
label: "not supported".to_string(),
|
||||||
secondary_label: None,
|
span: InnerSpan::new(
|
||||||
suggestion: Suggestion::UsePositional,
|
arg.position_span.start,
|
||||||
},
|
field.position_span.end,
|
||||||
);
|
),
|
||||||
}
|
secondary_label: None,
|
||||||
|
suggestion: Suggestion::UsePositional,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
ArgumentIs(_) => {
|
||||||
|
self.errors.insert(
|
||||||
|
0,
|
||||||
|
ParseError {
|
||||||
|
description: "tuple index access isn't supported".to_string(),
|
||||||
|
note: None,
|
||||||
|
label: "not supported".to_string(),
|
||||||
|
span: InnerSpan::new(
|
||||||
|
arg.position_span.start,
|
||||||
|
field.position_span.end,
|
||||||
|
),
|
||||||
|
secondary_label: None,
|
||||||
|
suggestion: Suggestion::UsePositional,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
10
tests/ui/fmt/format-args-non-identifier-diagnostics.fixed
Normal file
10
tests/ui/fmt/format-args-non-identifier-diagnostics.fixed
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
// 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>.
|
||||||
|
//@ run-rustfix
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = (1,);
|
||||||
|
println!("{0}", x.0);
|
||||||
|
//~^ ERROR invalid format string
|
||||||
|
}
|
10
tests/ui/fmt/format-args-non-identifier-diagnostics.rs
Normal file
10
tests/ui/fmt/format-args-non-identifier-diagnostics.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
// 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>.
|
||||||
|
//@ run-rustfix
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = (1,);
|
||||||
|
println!("{x.0}");
|
||||||
|
//~^ ERROR invalid format string
|
||||||
|
}
|
13
tests/ui/fmt/format-args-non-identifier-diagnostics.stderr
Normal file
13
tests/ui/fmt/format-args-non-identifier-diagnostics.stderr
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
error: invalid format string: tuple index access isn't supported
|
||||||
|
--> $DIR/format-args-non-identifier-diagnostics.rs:8:16
|
||||||
|
|
|
||||||
|
LL | println!("{x.0}");
|
||||||
|
| ^^^ not supported in format string
|
||||||
|
|
|
||||||
|
help: consider using a positional formatting argument instead
|
||||||
|
|
|
||||||
|
LL | println!("{0}", x.0);
|
||||||
|
| ~ +++++
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
Loading…
Reference in New Issue
Block a user