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

poor formatting of some if/else expressions with hard_tabs = true #4575

Open
nitsky opened this issue Dec 3, 2020 · 2 comments
Open

poor formatting of some if/else expressions with hard_tabs = true #4575

nitsky opened this issue Dec 3, 2020 · 2 comments
Assignees
Labels
a-binop only-with-option requires a non-default option value to reproduce p-low poor-formatting

Comments

@nitsky
Copy link

nitsky commented Dec 3, 2020

With hard_tabs = true, the formatting of some if/else expressions is shifted one level of indentation to the left. Everything works as expected when hard_tabs = false.

Input

fn main() {
    let condition_a = true;
    let condition_b = false;
    let x = 123.456789
        + if condition_a {
            x + y + z + w
        } else {
            123.456789
        }
        + if condition_b {
            x - y - z - w
        } else {
            123.456789
        };
}

Output

The issue is the last three lines of the main() function are indented one level less than expected.

fn main() {
	let condition_a = true;
	let condition_b = false;
	let x = 123.456789
		+ if condition_a {
			x + y + z + w
		} else {
			123.456789
		} + if condition_b {
		x - y - z - w
	} else {
		123.456789
	};
}

Expected output

fn main() {
	let condition_a = true;
	let condition_b = false;
	let x = 123.456789
		+ if condition_a {
			x + y + z + w
		} else {
			123.456789
		} + if condition_b {
			x - y - z - w
		} else {
			123.456789
		};
}

Meta

  • rustfmt version: rustfmt 1.4.24-stable (eb894d5 2020-11-05)
  • From where did you install rustfmt?: rustup
@calebcartwright calebcartwright added a-binop only-with-option requires a non-default option value to reproduce labels Dec 6, 2020
@workingjubilee
Copy link
Member

As with #3151 and #3251, this produces a lot of spaces when hard_tabs = true is combined with the indent_style = "Visual" option, with no improvement on the formatting itself.

fn main() {
	let condition_a = true;
	let condition_b = false;
	let x = 123.456789
	        + if condition_a {
		        x + y + z + w
	        } else {
		        123.456789
	        } + if condition_b {
		x - y - z - w
	} else {
		123.456789
	};
}

screenshot:
image

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

@dhouck
Copy link

dhouck commented Nov 23, 2021

The documentation says itʼs supposed to produce those spaces

hard_tabs

Use tab characters for indentation, spaces for alignment

That way, things that are visually aligned stay visually aligned regardless of the tabstop. It doesnʼt look aligned on your screenshot because IDEA added : f64 to the code and rustfmt canʼt know about that to account for it in alignment, but if you donʼt have that it does. Consider the code block right above the screenshot, as seen on my computer:
image
There, the + on line 5 is aligned right below the 1 on line 4, even though by browser uses 8-space tabs instead of the 4-space ones rustfmt would assume.

Even if this were the wrong behavior, itʼs unrelated to this issue; Iʼm seeing it with 1.4.37-stable with the default indent_style:

fn main() {
	let condition_a = true;
	let condition_b = false;
	let x = 123.456789
		+ if condition_a {
			x + y + z + w
		} else {
			123.456789
		} + if condition_b {
		x - y - z - w
	} else {
		123.456789
	};
}
Screenshot ![image](https://user-images.githubusercontent.com/524925/142982998-978c7385-4286-4721-8666-8f1d43fb25ea.png)

@ytmimi ytmimi added the p-low label Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-binop only-with-option requires a non-default option value to reproduce p-low poor-formatting
Projects
None yet
Development

No branches or pull requests

6 participants