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:
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.rsMeta
Also reproduced on 1141ee711fc87bf51ff8934c09a9d608b34e50bf (a recent HEAD of master)
$ rustfmt --version
rustfmt 1.10.0-nightly (bff8e12ff5 2026-08-26)Source: rust-lang/rustfmt