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

First and second operand in series of binops should not be on one line if first operand 4 characters and binop requires surrounding spaces #6314

Open
joshtriplett opened this issue Sep 4, 2024 · 0 comments

Comments

@joshtriplett
Copy link
Member

We have a rule that puts the second operand of a chain on the same line as the first if the first is 4 characters or less:

fn foo() -> Type {
    xxxx.longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong()
        .more()
        .things?
        .others();
}

This rule avoids adding an extra line break when there was room on the same line.

We apply this rule to a series of binops, as well, which gives similarly satisfying results if the first operand is 1-3 characters long:

fn foo() -> Type {
    xxx + longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
        + more
        + things
        + others
}

However, we use the same 4-character threshold, without taking the space before the binop into account:

fn foo() -> Type {
    xxxx + longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
        + more
        + things
        + others
}

We only break to a new line when the first item becomes 5 or more characters long.

I think the rule here should be taking the whitespace before the operator into account, such that the threshold is "4 characters (including any space before the binop)".

I think the underlying rule from the style guide here is:

If the length of the last line of the first element plus its indentation is less than or equal to the indentation of the second line, then combine the first and second lines if they fit. Apply this rule recursively.

Worth noting that that rule is only defined for chains:

A chain is a sequence of field accesses, method calls, and/or uses of the try operator ?. E.g., a.b.c().d or foo?.bar().baz?.

I think it's good that we apply this to binops as well, even though we don't have a style guide rule for that. I think we should capture that generalization in the style guide. However, I also think that generalization needs to take into account whether the operator requires spaces around it (like + or -) or does not (like .).

cc @rust-lang/style

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

1 participant