rustfmt
rustfmt copied to clipboard
Add leading/trailing zeros to floats
Essentially, these reformats should be done for consistency and readability:
x. -> x.0
.x -> 0.x
So 1. would become 1.0 and .1 would become 0.1.
Sent a pull request to implement this.
Note that .x is not a concern, since it's not a valid floating-point literal in Rust (in contrast to C++).
Also I believe it would be nice to support different formatting styles. I came up with the following options:
Preserve(default). Leave the literal as-is.Always. Add a trailing zero to the literal. E.g.1.0, 2.0e10, 3.0f32.IfNoPostfix. Add a trailing zero by default. If the literal contains an exponent or a suffix, the zero and the preceding period are removed. E.g.1.0, 2e10, 3f32.Never. Remove the trailing zero. If the literal contains an exponent or a suffix, the preceding period is also removed. E.g.1., 2e10, 3f32.
Thanks for taking the time to look into this and for submitting a PR! I think this is a configuration rustfmt could support, and the different options you've outlined here make sense to me. I'll try to carve out some time over the next few days to review and provide feedback on your implementation 😁