Suggest to bind self.x to x when field x may be in format string by xizheyin · Pull Request #141633 · rust-lang/rust (original) (raw)

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.

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 format!("{thing.field}") that suggests what you say. So I guess if we just revert #141213 then they'll eventually get there:

With that PR reverted, users will go from
format!("{x}") to format!("{self.x}") to format!("{}", self.x).

Not that elegant, but it works.