rust-lang / rust-lang/rustfmt

Add disambiguating space after unary minus

Open
#5,949 3 comments 0 reactions 1 assignee View on GitHub

@chfogelman is already working on this.

Since Oct 27, 2023.

C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Currently, rustfmt combines unary minus with the expression it's being applied to. This can lead to ambiguity when it's followed by a method call on an integer literal. For example, -1.method() is parsed by rustc as -(1.method()) rather than (-1).method(). To reduce confusion, one might want to see a space between the unary minus and its operand, if the operand is a non-parethesized method invocation on a numeric literal. For example:

-1;
-x;
-(x + y);
-x.y;
-x.method();
-(1.method());
// but
- 1.method();

This issue is motivated by: https://github.com/rust-lang/rust/issues/117155

I'm happy to claim this and implement it, as long as maintainers think it's a good idea.

@rustbot claim

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.