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

Arrays, call chains, ident_style and hard_tabs #3251

Open
DarthGandalf opened this issue Dec 16, 2018 · 2 comments
Open

Arrays, call chains, ident_style and hard_tabs #3251

DarthGandalf opened this issue Dec 16, 2018 · 2 comments
Labels
a-chains only-with-option requires a non-default option value to reproduce poor-formatting

Comments

@DarthGandalf
Copy link

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)

@topecongiro topecongiro added poor-formatting only-with-option requires a non-default option value to reproduce labels Dec 17, 2018
@workingjubilee
Copy link
Member

workingjubilee commented Apr 6, 2021

Something smells funny: hard_tabs = true + indent_style = "Visual" appears to be resulting in quite a lot of "soft" (spaces-defined) tabs here.

fn main() {
	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(|| todo!())
		                     .filter(|| todo!())
		                     .map(|| todo!())
		                     .collect();
	}
}

screenshot:
image

This problem goes away with #indent_style commented out: everything aligns to hard tab stops as expected.

Currently:

>rustfmt --version
rustfmt 1.4.36-nightly (7de6968e 2021-02-07) # from rustup

@ytmimi
Copy link
Contributor

ytmimi commented Jul 20, 2022

Confirming that this is still reproducible with rustfmt 1.5.1-nightly (f2c31ba0 2022-07-19)

Input

fn foo() {
    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(|| todo!())
    .filter(|| todo!())
    .map(|| todo!())
    .collect();
}

Output

fn foo() {
	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(|| todo!())
	                     .filter(|| todo!())
	                     .map(|| todo!())
	                     .collect();
}

Linking the tracking issue for indent_style #3346 hard_tabs is stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-chains only-with-option requires a non-default option value to reproduce poor-formatting
Projects
None yet
Development

No branches or pull requests

5 participants