Skip to content

Commit 04f3a60

Browse files
committed
add flag to force newlines in let else statements
1 parent 86261bf commit 04f3a60

File tree

8 files changed

+692
-1
lines changed

8 files changed

+692
-1
lines changed

Configurations.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,58 @@ use core::slice;
17311731
#[cfg(feature = "alloc")] use core::slice;
17321732
```
17331733

1734+
## `let_else_force_newline`
1735+
1736+
Force `else` branch of a `let_else` expression to always come after a newline.
1737+
1738+
- **Default value**: `false`
1739+
- **Possible values**: `true`, `false`
1740+
- **Stable**: No (Tracking issue: [TODO](todo))
1741+
1742+
#### `false` (default):
1743+
1744+
```rust
1745+
fn main() {
1746+
let Some(w) = opt else { return Ok(()) };
1747+
1748+
let Some(x) = opt else { return };
1749+
1750+
let Some(y) = opt else {
1751+
return;
1752+
};
1753+
1754+
let Some(z) = some_very_very_very_very_long_name else {
1755+
return;
1756+
};
1757+
}
1758+
```
1759+
1760+
#### `true`:
1761+
1762+
```rust
1763+
fn main() {
1764+
let Some(w) = opt
1765+
else {
1766+
return Ok(());
1767+
};
1768+
1769+
let Some(x) = opt
1770+
else {
1771+
return;
1772+
};
1773+
1774+
let Some(y) = opt
1775+
else {
1776+
return;
1777+
};
1778+
1779+
let Some(z) = some_very_very_very_very_long_name
1780+
else {
1781+
return;
1782+
};
1783+
}
1784+
```
1785+
17341786
## `match_arm_blocks`
17351787

17361788
Controls whether arm bodies are wrapped in cases where the first line of the body cannot fit on the same line as the `=>` operator.

src/config/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ create_config! {
161161
format_generated_files: FormatGeneratedFiles, false, "Format generated files";
162162
generated_marker_line_search_limit: GeneratedMarkerLineSearchLimit, false, "Number of lines to \
163163
check for a `@generated` marker when `format_generated_files` is enabled";
164+
let_else_force_newline: LetElseForceNewline, true, "Always put the else branch of let_else \
165+
statements on a new line.";
164166

165167
// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
166168
merge_derives: MergeDerives, true, "Merge multiple `#[derive(...)]` into a single one";
@@ -820,6 +822,7 @@ version = "One"
820822
inline_attribute_width = 0
821823
format_generated_files = true
822824
generated_marker_line_search_limit = 5
825+
let_else_force_newline = false
823826
merge_derives = true
824827
use_try_shorthand = false
825828
use_field_init_shorthand = false
@@ -912,6 +915,7 @@ version = "Two"
912915
inline_attribute_width = 0
913916
format_generated_files = true
914917
generated_marker_line_search_limit = 5
918+
let_else_force_newline = false
915919
merge_derives = true
916920
use_try_shorthand = false
917921
use_field_init_shorthand = false

src/config/options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ config_option_with_style_edition_default!(
683683
InlineAttributeWidth, usize, _ => 0;
684684
FormatGeneratedFiles, bool, _ => true;
685685
GeneratedMarkerLineSearchLimit, usize, _ => 5;
686+
LetElseForceNewline, bool, _ => false;
686687

687688
// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
688689
MergeDerives, bool, _ => true;

src/items.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ impl Rewrite for ast::Local {
144144
result.as_str()
145145
};
146146
let force_newline_else = pat_str.contains('\n')
147-
|| !same_line_else_kw_and_brace(init_str, context, else_kw_span, nested_shape);
147+
|| !same_line_else_kw_and_brace(init_str, context, else_kw_span, nested_shape)
148+
|| context.config.let_else_force_newline();
148149
let else_kw = rewrite_else_kw_with_comments(
149150
force_newline_else,
150151
true,
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// rustfmt-let_else_force_newline: true
2+
3+
fn main() {
4+
// Although this won't compile it still parses so make sure we can format empty else blocks
5+
let Some(x) = opt else {};
6+
7+
// let-else may be formatted on a single line if they are "short"
8+
// and only contain a single expression
9+
let Some(x) = opt else { return };
10+
11+
let Some(x) = opt else {
12+
return
13+
};
14+
15+
let Some(x) = opt else { return; };
16+
17+
let Some(x) = opt else {
18+
// nope
19+
return;
20+
};
21+
22+
let Some(x) = opt else { let y = 1; return y };
23+
24+
let Some(x) = y.foo("abc", fairly_long_identifier, "def", "123456", "string", "cheese") else { bar() };
25+
26+
let Some(x) = abcdef().foo("abc", some_really_really_really_long_ident, "ident", "123456").bar().baz().qux("fffffffffffffffff") else { foo_bar() };
27+
}
28+
29+
fn with_comments_around_else_keyword() {
30+
let Some(x) = opt /* pre else keyword block-comment */ else { return };
31+
32+
let Some(x) = opt else /* post else keyword block-comment */ { return };
33+
34+
let Some(x) = opt /* pre else keyword block-comment */ else /* post else keyword block-comment */ { return };
35+
36+
let Some(x) = opt // pre else keyword line-comment
37+
else { return };
38+
39+
let Some(x) = opt else
40+
// post else keyword line-comment
41+
{ return };
42+
43+
let Some(x) = opt // pre else keyword line-comment
44+
else
45+
// post else keyword line-comment
46+
{ return };
47+
48+
}
49+
50+
fn unbreakable_initializer_expr_pre_formatting_let_else_length_near_max_width() {
51+
// Pre Formatting:
52+
// The length of `(indent)let pat = init else block;` is 100 (max_width)
53+
// Post Formatting:
54+
// The formatting is left unchanged!
55+
let Some(x) = some_really_really_really_really_really_really_really_long_name_A else { return };
56+
57+
// Pre Formatting:
58+
// The length of `(indent)let pat = init else block;` is 100 (max_width)
59+
// Post Formatting:
60+
// The else keyword and opening brace remain on the same line as the initializer expr,
61+
// and the else block is formatted over multiple lines because we can't fit the
62+
// else block on the same line as the initializer expr.
63+
let Some(x) = some_really_really_really_really_really_really_really_long_name___B else {return};
64+
65+
// Pre Formatting:
66+
// The length of `(indent)let pat = init else block;` is 100 (max_width)
67+
// Post Formatting:
68+
// The else keyword and opening brace remain on the same line as the initializer expr,
69+
// and the else block is formatted over multiple lines because we can't fit the
70+
// else block on the same line as the initializer expr.
71+
let Some(x) = some_really_really_really_really_long_name_____C else {some_divergent_function()};
72+
73+
// Pre Formatting:
74+
// The length of `(indent)let pat = init else block;` is 101 (> max_width)
75+
// Post Formatting:
76+
// The else keyword and opening brace remain on the same line as the initializer expr,
77+
// and the else block is formatted over multiple lines because we can't fit the
78+
// else block on the same line as the initializer expr.
79+
let Some(x) = some_really_really_really_really_really_really_really_long_name__D else { return };
80+
}
81+
82+
fn unbreakable_initializer_expr_pre_formatting_length_up_to_opening_brace_near_max_width() {
83+
// Pre Formatting:
84+
// The length of `(indent)let pat = init else {` is 99 (< max_width)
85+
// Post Formatting:
86+
// The else keyword and opening brace remain on the same line as the initializer expr,
87+
// and the else block is formatted over multiple lines because we can't fit the
88+
// else block on the same line as the initializer expr.
89+
let Some(x) = some_really_really_really_really_really_really_really_really_long_name___E else {return};
90+
91+
// Pre Formatting:
92+
// The length of `(indent)let pat = init else {` is 101 (> max_width)
93+
// Post Formatting:
94+
// The else keyword and opening brace cannot fit on the same line as the initializer expr.
95+
// They are formatted on the next line.
96+
let Some(x) = some_really_really_really_really_really_really_really_really_long_name_____F else {return};
97+
}
98+
99+
fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_near_max_width() {
100+
// Pre Formatting:
101+
// The length of `(indent)let pat = init` is 99 (< max_width)
102+
// Post Formatting:
103+
// The else keyword and opening brace cannot fit on the same line as the initializer expr.
104+
// They are formatted on the next line.
105+
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name___G else {return};
106+
107+
// Pre Formatting:
108+
// The length of `(indent)let pat = init` is 100 (max_width)
109+
// Post Formatting:
110+
// Break after the `=` and put the initializer expr on it's own line.
111+
// Because the initializer expr is multi-lined the else is placed on it's own line.
112+
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name____H else {return};
113+
114+
// Pre Formatting:
115+
// The length of `(indent)let pat = init` is 109 (> max_width)
116+
// Post Formatting:
117+
// Break after the `=` and put the initializer expr on it's own line.
118+
// Because the initializer expr is multi-lined the else is placed on it's own line.
119+
// The initializer expr has a length of 91, which when indented on the next line
120+
// The `(indent)init` line has a length of 99. This is the max length that the `init` can be
121+
// before we start running into max_width issues. I suspect this is because the shape is
122+
// accounting for the `;` at the end of the `let-else` statement.
123+
let Some(x) = some_really_really_really_really_really_really_really_really_really_really_long_name______I else {return};
124+
125+
// Pre Formatting:
126+
// The length of `(indent)let pat = init` is 110 (> max_width)
127+
// Post Formatting:
128+
// Max length issues prevent us from formatting.
129+
// The initializer expr has a length of 92, which if it would be indented on the next line
130+
// the `(indent)init` line has a length of 100 which == max_width of 100.
131+
// One might expect formatting to succeed, but I suspect the reason we hit max_width issues is
132+
// because the Shape is accounting for the `;` at the end of the `let-else` statement.
133+
let Some(x) = some_really_really_really_really_really_really_really_really_really_really_really_long_nameJ else {return};
134+
}
135+
136+
fn long_patterns() {
137+
let Foo {x: Bar(..), y: FooBar(..), z: Baz(..)} = opt else {
138+
return;
139+
};
140+
141+
// with version=One we don't wrap long array patterns
142+
let [aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, cccccccccccccccccc, dddddddddddddddddd] = opt else {
143+
return;
144+
};
145+
146+
let ("aaaaaaaaaaaaaaaaaaa" | "bbbbbbbbbbbbbbbbb" | "cccccccccccccccccccccccc" | "dddddddddddddddd" | "eeeeeeeeeeeeeeee") = opt else {
147+
return;
148+
};
149+
150+
let Some(Ok((Message::ChangeColor(super::color::Color::Rgb(r, g, b)), Point { x, y, z }))) = opt else {
151+
return;
152+
};
153+
}
154+
155+
fn with_trailing_try_operator() {
156+
// Currently the trailing ? forces the else on the next line
157+
// This may be revisited in style edition 2024
158+
let Some(next_bucket) = ranking_rules[cur_ranking_rule_index].next_bucket(ctx, logger, &ranking_rule_universes[cur_ranking_rule_index])? else { return };
159+
160+
// Maybe this is a workaround?
161+
let Ok(Some(next_bucket)) = ranking_rules[cur_ranking_rule_index].next_bucket(ctx, logger, &ranking_rule_universes[cur_ranking_rule_index]) else { return };
162+
}
163+
164+
fn issue5901() {
165+
#[cfg(target_os = "linux")]
166+
let Some(x) = foo else { todo!() };
167+
168+
#[cfg(target_os = "linux")]
169+
// Some comments between attributes and let-else statement
170+
let Some(x) = foo else { todo!() };
171+
172+
#[cfg(target_os = "linux")]
173+
#[cfg(target_arch = "x86_64")]
174+
let Some(x) = foo else { todo!() };
175+
176+
// The else block will be multi-lined because attributes and comments before `let`
177+
// are included when calculating max width
178+
#[cfg(target_os = "linux")]
179+
#[cfg(target_arch = "x86_64")]
180+
// Some comments between attributes and let-else statement
181+
let Some(x) = foo() else { todo!() };
182+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// rustfmt-style_edition: 2024
2+
// rustfmt-let_else_force_newline: true
3+
4+
fn issue5901() {
5+
#[cfg(target_os = "linux")]
6+
let Some(x) = foo else { todo!() };
7+
8+
#[cfg(target_os = "linux")]
9+
// Some comments between attributes and let-else statement
10+
let Some(x) = foo else { todo!() };
11+
12+
#[cfg(target_os = "linux")]
13+
#[cfg(target_arch = "x86_64")]
14+
let Some(x) = foo else { todo!() };
15+
16+
// The else block is multi-lined
17+
#[cfg(target_os = "linux")]
18+
let Some(x) = foo else { return; };
19+
20+
// The else block will be single-lined because attributes and comments before `let`
21+
// are no longer included when calculating max width
22+
#[cfg(target_os = "linux")]
23+
#[cfg(target_arch = "x86_64")]
24+
// Some comments between attributes and let-else statement
25+
let Some(x) = foo else { todo!() };
26+
27+
// Some more test cases for v2 formatting with attributes
28+
29+
#[cfg(target_os = "linux")]
30+
#[cfg(target_arch = "x86_64")]
31+
let Some(x) = opt
32+
// pre else keyword line-comment
33+
else { return; };
34+
35+
#[cfg(target_os = "linux")]
36+
#[cfg(target_arch = "x86_64")]
37+
let Some(x) = opt else
38+
// post else keyword line-comment
39+
{ return; };
40+
41+
#[cfg(target_os = "linux")]
42+
#[cfg(target_arch = "x86_64")]
43+
let Foo {x: Bar(..), y: FooBar(..), z: Baz(..)} = opt else {
44+
return;
45+
};
46+
47+
#[cfg(target_os = "linux")]
48+
#[cfg(target_arch = "x86_64")]
49+
let Some(Ok((Message::ChangeColor(super::color::Color::Rgb(r, g, b)), Point { x, y, z }))) = opt else {
50+
return;
51+
};
52+
53+
#[cfg(target_os = "linux")]
54+
#[cfg(target_arch = "x86_64")]
55+
let Some(x) = very_very_very_very_very_very_very_very_very_very_very_very_long_expression_in_assign_rhs() else { return; };
56+
}

0 commit comments

Comments
 (0)