Skip to content

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

Description

@matthewhughes934

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-call-chainsArea: method call chainsA-commentsArea: commentsC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICESO-use_try_shorthandStable option: use_try_shorthandneeds-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions