xls icon indicating copy to clipboard operation
xls copied to clipboard

Continuation of expression removes indentation

Open hzeller opened this issue 2 years ago • 2 comments

Consider the following with a long expression that is continued at the next line with appropriate indentation:

fn foo(some_value: u32) -> u32 {
    some_value + some_value + some_value + some_value + some_value + some_value + some_value +
        some_value
}

The formatter removes this indentation of the continuation and starts the continuation in the column the original expression started:

fn foo(some_value: u32) -> u32 {
    some_value + some_value + some_value + some_value + some_value + some_value + some_value +
    some_value
}

I suspect aligning the continuation with the beginning of the original expression is only an issue in free-standing expressions used as a 'return from something' situation. In assignments, the current behavior produces good results as the indentation is given by the assignment in the line above:

fn foo(some_value: u32) {
    let a = some_value + some_value + some_value + some_value + some_value + some_value + some_value +
            some_value;
}

hzeller avatar Nov 16 '23 22:11 hzeller

The let statement is what causes the RHS to be at the alignment of the = -- I'm not sure if it makes sense to align all expressions that overflow a single line one nesting level?

cdleary avatar Nov 17 '23 00:11 cdleary

I think indentation in continuation of expressions will improve readability.

Compare the first (hand-written) and second (formatter generated) example. The second looks like foo() just returns some_value as it hides the fact that it is a continuation and only if the reader explicitly looks at the end of the previous line sees that this is actually part of a long expression (we have seen how subtle things at the end of lines can be easily overlooked by readers of code...)

(that is why I personally also advocate to move the operator that continuates in the new line in other code, but I guess that ship has sailed).

(The let statement is perfectly indented and should not change; the LHS forces indentation of everything on the RHS, so that makes clear what is happening)

hzeller avatar Nov 20 '23 22:11 hzeller

This is definitely still broken.

dplassgit avatar Jan 06 '25 16:01 dplassgit