#7101·rustfmt

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

Author: matthewhughes934Created Sep 5, 2026Updated Sep 8, 2026
LabelsC-bugA-commentsA-call-chainsI-invalid-codeSO-use_try_shorthand

Summary

Just recording something I ran into during my (ongoing) dive into chain formatting

I tried to format this code:

rust
fn main() {
    let _ = r#try!(root)/* this is a comment*/.bar;
}

Expected behavior

It would be formatted to something like

rust
fn main() {
    let _ = root? /* this is a comment*/
        .bar;
}

Actual behavior

It was formatted to invalid code:

rust
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)