Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Rust format might break ranges in specific cases #6059

Closed
PaulDotSH opened this issue Feb 7, 2024 · 2 comments · Fixed by #6085
Closed

[BUG] Rust format might break ranges in specific cases #6059

PaulDotSH opened this issue Feb 7, 2024 · 2 comments · Fixed by #6085
Labels
bug Panic, non-idempotency, invalid code, etc.

Comments

@PaulDotSH
Copy link

PaulDotSH commented Feb 7, 2024

In this code snippet

        self.coords.x -= rng.gen_range(-self.radius / 2. .. self.radius / 2.);

Running rust format will change this code to

        self.coords.x -= rng.gen_range(-self.radius / 2...self.radius / 2.);

Which will make rustc unable to compile the file because 2...X is not a valid syntax for ranges, however having the space lets rustc know that 2 is supposed to be a float and the dots represent a range.

Proposed fix:
Either do not remove the spaces in this case, or add a 0 in the case of floats.

@ytmimi ytmimi added the bug Panic, non-idempotency, invalid code, etc. label Feb 7, 2024
@ytmimi
Copy link
Contributor

ytmimi commented Feb 7, 2024

Thanks for the report!

There's currently a proposal to add an option to rustfmt to always add 0 to the end of float literals (#5834). We've also had similar issues in the past (#5791 for example and #4808 is similarly related), and in those cases we resolved the issue by wrapping the item with the trialing . in parenthesis. We might need to do the same thing here.

In the meantime it probably makes sense to manually add a 0 to work around this issue.

@ytmimi
Copy link
Contributor

ytmimi commented Feb 16, 2024

I've also realized that we have a spaces_around_ranges option that could also be used to mitigate this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants