Suggest to bind self.x to x when field x may be in format string#141633
Suggest to bind self.x to x when field x may be in format string#141633rust-bors[bot] merged 1 commit intorust-lang:mainfrom
self.x to x when field x may be in format string#141633Conversation
|
Please choose another assignee. |
|
r? @davidtwco rustbot has assigned @davidtwco. Use |
davidtwco
left a comment
There was a problem hiding this comment.
I don't think this is an improvement, the help message being changed is just as correct as what you are suggesting, but feels more appropriate as it adds to the format macro that is where the error originates rather than adding another line entirely. You could attempt to improve this with a structured suggestion that the user add a , foo = self.foo argument to the format macro.
|
For structured suggestions, I tried, but I don't currently have a better way to get the HIR or ast of the format macro, in this diagnostic I can only get the span of x. There is so little information given here. Do you have a good idea? |
|
The original help message is wrong for the following example So, a line of binding had to be added to ensure that the formatted string and this case in the example were satisfied. |
The problem is that there's not a good way to detect that we're in the format (or write, print etc) macro to begin with. But format string parsing has a specialized error path for With that PR reverted, users will go from Not that elegant, but it works. |
|
But if we suggest creating a new variable, the suggestion is always correct; let x = &self.x;
format!("{x}"); |
I'm more curious if this is considered proper advice if it's suggested through a two-paragraph application?
Yes, this prompts the user once and avoids having to apply the suggestion twice. |
It's not, but you also have to weigh that against how accurate the suggestion is and how likely someone is to run into it to begin with. Going through code snippets and looking for braces is a hacky thing that risks being inaccurate. |
Does |
|
I tried this and it was false. Probably because it's not what the macro expands out to, but is simply treated as a variable. |
|
This PR seems to be on hold for a while, maybe David doesn't have time on this PR for a while. |
|
|
|
I'll re-examine this PR these days. I recall that #142594 couldn't help because of #142594 (comment). |
|
☔ The latest upstream changes (presumably #149836) made this pull request unmergeable. Please resolve the merge conflicts. |
|
@xizheyin any updates on this? thanks |
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
| Ok(s.get(start - 1..start) == Some("{")) | ||
| }); | ||
| if let Ok(true) = field_is_format_named_arg { | ||
| Ok(s.get(start.saturating_sub(1)..start) == Some("{")) |
There was a problem hiding this comment.
Here is a small fix, because the start may be 0 in some cases.
| let field_is_format_named_arg = source_map | ||
| let field_is_format_named_arg = matches!( | ||
| span.desugaring_kind(), | ||
| Some(DesugaringKind::FormatLiteral { .. }) |
There was a problem hiding this comment.
Here we check FormatLiteral.
|
@rustbot ready |
|
@bors r+ |
Suggest to bind `self.x` to `x` when field `x` may be in format string Fixes rust-lang#141350 I added the new test in the first commit, and committed the changes in the second one. r? @fmease cc @mejrs
Suggest to bind `self.x` to `x` when field `x` may be in format string Fixes rust-lang#141350 I added the new test in the first commit, and committed the changes in the second one. r? @fmease cc @mejrs
Rollup of 19 pull requests Successful merges: - #141633 (Suggest to bind `self.x` to `x` when field `x` may be in format string) - #152980 (c-variadic: fix implementation on `avr`) - #154491 (Extend `core::char`'s documentation of casing issues (and fix a rustdoc bug)) - #155318 (Use mutable pointers for Unix path buffers) - #155335 (Bump bootstrap to 1.96 beta) - #155354 (Remove AttributeSafety from BUILTIN_ATTRIBUTES) - #154970 (rustdoc: preserve `doc(cfg)` on locally re-exported type aliases) - #155095 (changed the information provided by (mut x) to mut x (Fix 155030)) - #155305 (Make `convert_while_ascii` unsafe) - #155358 (ImproperCTypes: Move erasing_region_normalisation into helper function) - #155377 (tests/debuginfo/basic-stepping.rs: Remove FIXME related to ZSTs) - #155383 (Rearrange `rustc_ast_pretty`) - #155384 (triagebot: notify on diagnostic attribute changes) - #155386 (Use `box_new` diagnostic item for Box::new suggestions) - #155391 (Small refactor of `QueryJob::latch` method) - #155395 (Tweak how the "copy path" rustdoc button works to allow some accessibility tool to work with rustdoc) - #155396 (`as_ref_unchecked` docs link fix) - #155411 (compiletest: Remove the `//@ should-ice` directive) - #155413 (fix: typo in `std::fs::hard_link` documentation)
Rollup of 19 pull requests Successful merges: - rust-lang/rust#141633 (Suggest to bind `self.x` to `x` when field `x` may be in format string) - rust-lang/rust#152980 (c-variadic: fix implementation on `avr`) - rust-lang/rust#154491 (Extend `core::char`'s documentation of casing issues (and fix a rustdoc bug)) - rust-lang/rust#155318 (Use mutable pointers for Unix path buffers) - rust-lang/rust#155335 (Bump bootstrap to 1.96 beta) - rust-lang/rust#155354 (Remove AttributeSafety from BUILTIN_ATTRIBUTES) - rust-lang/rust#154970 (rustdoc: preserve `doc(cfg)` on locally re-exported type aliases) - rust-lang/rust#155095 (changed the information provided by (mut x) to mut x (Fix 155030)) - rust-lang/rust#155305 (Make `convert_while_ascii` unsafe) - rust-lang/rust#155358 (ImproperCTypes: Move erasing_region_normalisation into helper function) - rust-lang/rust#155377 (tests/debuginfo/basic-stepping.rs: Remove FIXME related to ZSTs) - rust-lang/rust#155383 (Rearrange `rustc_ast_pretty`) - rust-lang/rust#155384 (triagebot: notify on diagnostic attribute changes) - rust-lang/rust#155386 (Use `box_new` diagnostic item for Box::new suggestions) - rust-lang/rust#155391 (Small refactor of `QueryJob::latch` method) - rust-lang/rust#155395 (Tweak how the "copy path" rustdoc button works to allow some accessibility tool to work with rustdoc) - rust-lang/rust#155396 (`as_ref_unchecked` docs link fix) - rust-lang/rust#155411 (compiletest: Remove the `//@ should-ice` directive) - rust-lang/rust#155413 (fix: typo in `std::fs::hard_link` documentation)
View all comments
Fixes #141350
I added the new test in the first commit, and committed the changes in the second one.
r? @fmease
cc @mejrs