rust-lang / rust-lang/rustfmt

Chains of functions with a comment on the last function do not format

Open
#7,086 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-call-chains A-comments C-bug I-poor-formatting
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Summary

I tried to format this code:

Some(5i32)
.unwrap() // Comment
;
Expected behavior

I expected it to format to:

Some(5i32)
    .unwrap() // Comment
    ;
Actual behavior

Instead, it didn't format it at all. Looking into it more, it appears that the issue is just as the title suggests, any chain of function calls doesn't get formatted if there's a comment on the last one. While the given example is unrealistic, the bug also occurs if there's an as cast after the chain.

Configuration

Default

Reproduction Steps

Try to format the code in the playground.

Meta

rustfmt --version:

rustfmt 1.10.0-nightly (2026-08-26 bff8e12ff5)

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 the rustfmt playground, including the chained call with a trailing comment and the variant with an as cast. Trace the rustfmt formatting path for chained function calls; done means both cases format as expected 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.