rust-lang / rust-lang/rustfmt

Rewriting `r#try!` at root of chain generates bad code

Open
#7,101 3 comments 0 reactions 0 assignees View on GitHub

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

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.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.