Skip to content

Commit 0e8170d

Browse files
committed
Fix checking if newline is needed before else in let-else statement (#5901)
1 parent b636723 commit 0e8170d

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/items.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,17 @@ impl Rewrite for ast::Local {
127127

128128
if let Some(block) = else_block {
129129
let else_kw_span = init.span.between(block.span);
130+
let init_str = if !attrs_str.is_empty() {
131+
// Strip attributes to check if newline is needed before the else keyword from
132+
// the initializer part. (#5901)
133+
result
134+
.strip_prefix(&attrs_str)
135+
.map_or(result.as_str(), str::trim_start)
136+
} else {
137+
result.as_str()
138+
};
130139
let force_newline_else = pat_str.contains('\n')
131-
|| !same_line_else_kw_and_brace(&result, context, else_kw_span, nested_shape);
140+
|| !same_line_else_kw_and_brace(init_str, context, else_kw_span, nested_shape);
132141
let else_kw = rewrite_else_kw_with_comments(
133142
force_newline_else,
134143
true,

tests/source/let_else.rs

+5
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,8 @@ fn with_trailing_try_operator() {
160160
// Maybe this is a workaround?
161161
let Ok(Some(next_bucket)) = ranking_rules[cur_ranking_rule_index].next_bucket(ctx, logger, &ranking_rule_universes[cur_ranking_rule_index]) else { return };
162162
}
163+
164+
fn issue5901() {
165+
#[cfg(target_os = "linux")]
166+
let Some(x) = foo() else { return; };
167+
}

tests/target/let_else.rs

+7
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,10 @@ fn with_trailing_try_operator() {
252252
return;
253253
};
254254
}
255+
256+
fn issue5901() {
257+
#[cfg(target_os = "linux")]
258+
let Some(x) = foo() else {
259+
return;
260+
};
261+
}

0 commit comments

Comments
 (0)