Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chained call after block should be one level deeper #6332

Closed
upsuper opened this issue Sep 18, 2024 · 3 comments
Closed

Chained call after block should be one level deeper #6332

upsuper opened this issue Sep 18, 2024 · 3 comments

Comments

@upsuper
Copy link

upsuper commented Sep 18, 2024

Currently code like

    foo.call_some_method().do_something(lots, of, arguments).do_another_thing(also, many, arguments)

gets formatted as

    foo.call_some_method()
        .do_something(lots, of, arguments)
        .do_another_thing(also, many, arguments)

However, something like

    Foo { field_a: value_a, field_b: value_b }.call_some_method().do_something(lots, of, arguments).do_another_thing(also, many, arguments)

gets formatted as

    Foo {
        field_a: value_a,
        field_b: value_b,
    }
    .call_some_method()
    .do_something(lots, of, arguments)
    .do_another_thing(also, many, arguments)

Noting that the chained call is at the same depth as the block above.

This feels inconsistent, and I think it would be good if it can be formatted as

    Foo {
        field_a: value_a,
        field_b: value_b,
    }
        .call_some_method()
        .do_something(lots, of, arguments)
        .do_another_thing(also, many, arguments)

instead, or at least make it configurable.

On a separate note, if this whole thing is wrapped with another block, the formatting is even more weird, for example:

    vec![Foo {
        field_a: value_a,
        field_b: value_b,
    }
    .call_some_method()
    .do_something(lots, of, arguments)
    .do_another_thing(also, many, arguments)]

which I would expect to become something like

    vec![
        Foo {
            field_a: value_a,
            field_b: value_b,
        }
            .call_some_method()
            .do_something(lots, of, arguments)
            .do_another_thing(also, many, arguments),
    ]
@ytmimi
Copy link
Contributor

ytmimi commented Sep 18, 2024

The struct literal formatting you're highlighting above is consistent with what happens if a chain contains multi-line elements. For example take the following function call:

fn m() {
    bar(
        Some(value_a),
        Some(value_b),
        Some(value_c),
        Some(value_d),
        Some(value_e),
        Some(value_f),
        Some(value_g),
    )
    .call_some_method()
    .do_something(lots, of, arguments)
    .do_another_thing(also, many, arguments)
}

@upsuper
Copy link
Author

upsuper commented Sep 18, 2024

I'd say that is inconsistent with when it follows a single line as well. Why would the chain get indented when it starts with a single line, but not when it starts with a multi-line block?

@ytmimi
Copy link
Contributor

ytmimi commented Sep 24, 2024

Got a response to my question in rust-lang/style-team#195 (comment), and there's nothing to change on the rustfmt side.

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants