Rewriting `r#try!` at root of chain generates bad code
Open
Nobody has claimed this yet.
A-call-chains
A-comments
C-bug
I-invalid-code
SO-use_try_shorthand
- Dominant language
- Rust
- Stars
- 7k
- Forks
- 1.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 24
Description
Summary
Just recording something I ran into during my (ongoing) dive into chain formatting
I tried to format this code:
fn main() {
let _ = r#try!(root)/* this is a comment*/.bar;
}
Expected behavior
It would be formatted to something like
fn main() {
let _ = root? /* this is a comment*/
.bar;
}
Actual behavior
It was formatted to invalid code:
fn main() {
let _ = root? )/* this is a comment*/
.bar;
}
Configuration
Only needs the use_try_shorthand option
Reproduction Steps
rustfmt --config use_try_shorthand=true ./file.rs
Meta
Also reproduced on 1141ee711fc87bf51ff8934c09a9d608b34e50bf (a recent HEAD of master)
$ rustfmt --version
rustfmt 1.10.0-nightly (bff8e12ff5 2026-08-26)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the example in file.rs with rustfmt --config use_try_shorthand=true, then trace the chain-formatting path responsible for rewriting r#try! at the root. Done means the output is valid Rust and matches the expected root? chain formatting while preserving the comment.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100