Skip to content

Commit 64f70fc

Browse files
committed
Remove attributes and comments before let on calculating available space in let-else statement
1 parent dac46b7 commit 64f70fc

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

src/items.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,21 @@ impl Rewrite for ast::Local {
154154
std::cmp::min(shape.width, context.config.single_line_let_else_max_width());
155155

156156
// If available_space hits zero we know for sure this will be a multi-lined block
157-
let available_space = max_width.saturating_sub(result.len());
157+
let assign_len = if context.config.version() == Version::Two {
158+
result.len() - let_kw_offset
159+
} else {
160+
result.len()
161+
};
162+
let available_space = max_width.saturating_sub(assign_len);
158163

164+
let assign_str = if context.config.version() == Version::Two {
165+
&result[let_kw_offset..]
166+
} else {
167+
result.as_str()
168+
};
159169
let allow_single_line = !force_newline_else
160170
&& available_space > 0
161-
&& allow_single_line_let_else_block(&result, block);
171+
&& allow_single_line_let_else_block(assign_str, block);
162172

163173
let mut rw_else_block =
164174
rewrite_let_else_block(block, allow_single_line, context, shape)?;

tests/source/let_else.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,11 @@ fn issue5901() {
172172
#[cfg(target_os = "linux")]
173173
#[cfg(target_arch = "x86_64")]
174174
let Some(x) = foo else { return; };
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!() };
175182
}

tests/source/let_else_v2.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-version: Two
2+
// rustfmt-single_line_let_else_max_width: 100
23

34
fn issue5901() {
45
#[cfg(target_os = "linux")]
@@ -11,4 +12,11 @@ fn issue5901() {
1112
#[cfg(target_os = "linux")]
1213
#[cfg(target_arch = "x86_64")]
1314
let Some(x) = foo else { return; };
15+
16+
// The else block will be signle-lined because attributes and comments before `let`
17+
// are no longer included when calculating max width
18+
#[cfg(target_os = "linux")]
19+
#[cfg(target_arch = "x86_64")]
20+
// Some comments between attributes and let-else statement
21+
let Some(x) = foo else { todo!() };
1422
}

tests/target/let_else.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,13 @@ fn issue5901() {
273273
else {
274274
return;
275275
};
276+
277+
// The else block will be multi-lined because attributes and comments before `let`
278+
// are included when calculating max width
279+
#[cfg(target_os = "linux")]
280+
#[cfg(target_arch = "x86_64")]
281+
// Some comments between attributes and let-else statement
282+
let Some(x) = foo() else {
283+
todo!()
284+
};
276285
}

tests/target/let_else_v2.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-version: Two
2+
// rustfmt-single_line_let_else_max_width: 100
23

34
fn issue5901() {
45
#[cfg(target_os = "linux")]
@@ -17,4 +18,11 @@ fn issue5901() {
1718
let Some(x) = foo else {
1819
return;
1920
};
21+
22+
// The else block will be signle-lined because attributes and comments before `let`
23+
// are no longer included when calculating max width
24+
#[cfg(target_os = "linux")]
25+
#[cfg(target_arch = "x86_64")]
26+
// Some comments between attributes and let-else statement
27+
let Some(x) = foo else { todo!() };
2028
}

0 commit comments

Comments
 (0)