rust-lang / rust-lang/rustfmt

Arrays, call chains, ident_style and hard_tabs

Open
#3,251 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The method chain, starting with .iter() should be indented, but it's not.

fn foo(&self) {
    let coords: Vec<_> = [
        (self.y - 1, self.x),
        (self.y, self.x - 1),
        (self.y, self.x + 1),
        (self.y + 1, self.x),
    ]
    .iter()
    .filter_map(...)
    .filter(...)
    .map(...)
    .collect();
}

This can be somewhat mitigated by setting indent_style = "Visual", but:
a) I don't like that style very much, because the call chain becomes too indented (it looks worse when there are actual lambdas instead of ...)

fn foo(&self) {
    let coords: Vec<_> =
        [(self.y - 1, self.x),
         (self.y, self.x - 1),
         (self.y, self.x + 1),
         (self.y + 1, self.x)].iter()
                              .filter_map(...)
                              .filter(...)
                              .map(...)
                              .collect();
}

b) It conflicts with hard_tabs = true:

fn foo(&self) {
    let coords: Vec<_> = [
                          (self.y - 1, self.x),
                          (self.y, self.x - 1),
                          (self.y, self.x + 1),
                          (self.y + 1, self.x),
    ].iter()
                         .filter_map(...)
                         .filter(...)
                         .map(...)
                         .collect();
}

Combination of hard_tabs = true and indent_style = "Visual" chose to align some parts of the call chain (except the first one) with elements of the vector. As result, .iter() looks very disconnected from the rest.

This is similar to #3157 but has different trigger.

$ rustfmt --version
rustfmt 1.0.1-nightly (be13559 2018-12-10)

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 with rustfmt, then compare the output under the default and Visual indent_style settings with hard_tabs enabled. Trace the formatter's handling of array expressions followed by call chains; done means the reported chain indentation is corrected without the described Visual and hard-tabs regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.