From 57c32a193fee197f7ad034210de005db338a64ec Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 10 Jan 2024 19:30:22 -0800 Subject: [PATCH 1/3] style-guide: When breaking binops handle multi-line first operand better Use the indentation of the *last* line of the first operand, not the first. Fixes https://github.com/rust-lang/style-team/issues/189 --- src/doc/style-guide/src/editions.md | 2 ++ src/doc/style-guide/src/expressions.md | 31 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/doc/style-guide/src/editions.md b/src/doc/style-guide/src/editions.md index 9d593f80810..74e873e35ff 100644 --- a/src/doc/style-guide/src/editions.md +++ b/src/doc/style-guide/src/editions.md @@ -40,6 +40,8 @@ include: of a delimited expression, delimited expressions are generally combinable, regardless of the number of members. Previously only applied with exactly one member (except for closures with explicit blocks). +- When line-breaking a binary operator, if the first operand spans multiple + lines, use the base indentation of the last line. - Miscellaneous `rustfmt` bugfixes. - Use version-sort (sort `x8`, `x16`, `x32`, `x64`, `x128` in that order). - Change "ASCIIbetical" sort to Unicode-aware "non-lowercase before lowercase". diff --git a/src/doc/style-guide/src/expressions.md b/src/doc/style-guide/src/expressions.md index 171a24cd89d..597f8fcaf40 100644 --- a/src/doc/style-guide/src/expressions.md +++ b/src/doc/style-guide/src/expressions.md @@ -328,6 +328,37 @@ foo_bar Prefer line-breaking at an assignment operator (either `=` or `+=`, etc.) rather than at other binary operators. +If line-breaking a binary operator (including assignment operators) where the +first operand spans multiple lines, use the base indentation of the *last* +line of the first , and indent relative to that: + +```rust +impl SomeType { + fn method(&mut self) { + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info = + long_long_long_long_long_long_long_long_long_long_long_long_long_long_long; + + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info + + long_long_long_long_long_long_long_long_long_long_long_long_long_long_long; + + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info = Some(ExtraInfo { + parent, + count: count as u16, + children: children.into_boxed_slice(), + }); + } +} +``` + ### Casts (`as`) Format `as` casts like a binary operator. In particular, always include spaces From e2d9c0d938a057dfe745dea4a38eb397b6feb3c1 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 20 Jan 2024 13:26:01 -0800 Subject: [PATCH 2/3] Fix missing word Co-authored-by: Caleb Cartwright --- src/doc/style-guide/src/expressions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/style-guide/src/expressions.md b/src/doc/style-guide/src/expressions.md index 597f8fcaf40..221cb3fbcc3 100644 --- a/src/doc/style-guide/src/expressions.md +++ b/src/doc/style-guide/src/expressions.md @@ -330,7 +330,7 @@ than at other binary operators. If line-breaking a binary operator (including assignment operators) where the first operand spans multiple lines, use the base indentation of the *last* -line of the first , and indent relative to that: +line of the first operand, and indent relative to that: ```rust impl SomeType { From e098eb14ae9576593b38d4179c981fb7af7bee89 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 20 Jan 2024 13:26:26 -0800 Subject: [PATCH 3/3] Wording improvement Co-authored-by: Caleb Cartwright --- src/doc/style-guide/src/expressions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/style-guide/src/expressions.md b/src/doc/style-guide/src/expressions.md index 221cb3fbcc3..3bb0ee6d5ff 100644 --- a/src/doc/style-guide/src/expressions.md +++ b/src/doc/style-guide/src/expressions.md @@ -328,7 +328,7 @@ foo_bar Prefer line-breaking at an assignment operator (either `=` or `+=`, etc.) rather than at other binary operators. -If line-breaking a binary operator (including assignment operators) where the +If line-breaking at a binary operator (including assignment operators) where the first operand spans multiple lines, use the base indentation of the *last* line of the first operand, and indent relative to that: