rustfmt: rustfmt should avoid rightwards drifting big blocks of code
Currently rustfmt formats from
let mut arms = variants.iter().enumerate().map(|(i, &(ident, v_span, ref summary))| {
let i_expr = cx.expr_usize(v_span, i);
let pat = cx.pat_lit(v_span, i_expr);
let path = cx.path(v_span, vec![substr.type_ident, ident]);
let thing = rand_thing(cx, v_span, path, summary, |cx, sp| rand_call(cx, sp));
cx.arm(v_span, vec!( pat ), thing)
}).collect::<Vec<ast::Arm> >();
to
let mut arms = variants.iter()
.enumerate()
.map(|(i, &(ident, v_span, ref summary))| {
let i_expr = cx.expr_usize(v_span, i);
let pat = cx.pat_lit(v_span, i_expr);
let path = cx.path(v_span, vec![substr.type_ident, ident]);
let thing = rand_thing(cx,
v_span,
path,
summary,
|cx, sp| rand_call(cx, sp));
cx.arm(v_span, vec!(pat), thing)
})
.collect::<Vec<ast::Arm>>();
which is much worse than the original.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 7
- Comments: 25 (21 by maintainers)
I’m strongly in favor of using block indenting by default. In my experience alignment indenting has always simply caused problems for very little gain. Block indenting already serves the purpose of making it easy to see where things continue onto the next line, while alignment indenting just makes editing more difficult, causes noisy diffs, and results in horrible rightward drift that can induce even more newlines needed leading to pathological situations.
I’ve run across this many many times when working with rustfmt, and I agree with @nagisa’s original post that the first block of code is much more readable. We’ve often had a problem with rightward-drift of code in Rust in the past and we have tried to tailor idioms (like RAII) around preventing that. I personally prefer to have style wherever possible that avoids rightward drift as much as possible.
Block indent is just less complicated, and less likely to go wrong. Therefore, I think we should be using block formatting.
One thing I do do, which is very different from the vast majority of rust code, is to use 2-space indents; it fights against the rightward drift of rust quite well.
@nrc I think block indent (your second example) looks better, but more importantly, it also seems to cause fewer problems for rustfmt, fairly consistently (as this comment about a similar problem mentioned, alignment is not as scalable). On top of that, it’s an easier rule for a text editor to implement.
Aligned method calls should be an optional feature for those who are willing to risk more problematic formatting. I know this is against your personal preferences, but the defaults should be designed so rustfmt’s results are acceptable in the highest number of cases
rightwards drift is not in the number of spaces, but in the number of indentations.