Skip to content

Commit b276cb7

Browse files
committed
Add support for ExprKind::Let
1 parent 8b0b213 commit b276cb7

File tree

7 files changed

+80
-7
lines changed

7 files changed

+80
-7
lines changed

src/expr.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub(crate) fn format_expr(
127127
ast::ExprKind::Tup(ref items) => {
128128
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
129129
}
130-
ast::ExprKind::Let(..) => None,
130+
ast::ExprKind::Let(ref pat, ref expr, _span) => rewrite_let(context, shape, pat, expr),
131131
ast::ExprKind::If(..)
132132
| ast::ExprKind::ForLoop(..)
133133
| ast::ExprKind::Loop(..)
@@ -1779,6 +1779,30 @@ fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>(
17791779
Some(format!("({})", list_str))
17801780
}
17811781

1782+
fn rewrite_let(
1783+
context: &RewriteContext<'_>,
1784+
shape: Shape,
1785+
pat: &ast::Pat,
1786+
expr: &ast::Expr,
1787+
) -> Option<String> {
1788+
let mut result = "let ".to_owned();
1789+
1790+
// 4 = "let ".len()
1791+
let pat_shape = shape.offset_left(4)?;
1792+
let pat_str = pat.rewrite(context, pat_shape)?;
1793+
result.push_str(&pat_str);
1794+
1795+
result.push_str(" =");
1796+
1797+
rewrite_assign_rhs(
1798+
context,
1799+
result,
1800+
expr,
1801+
&RhsAssignKind::Expr(&expr.kind, expr.span),
1802+
shape,
1803+
)
1804+
}
1805+
17821806
pub(crate) fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
17831807
context: &'a RewriteContext<'_>,
17841808
items: impl Iterator<Item = &'a T>,

src/pairs.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ pub(crate) fn rewrite_all_pairs(
3434
shape: Shape,
3535
context: &RewriteContext<'_>,
3636
) -> Option<String> {
37-
expr.flatten(context, shape).and_then(|list| {
37+
let list = expr.flatten(context, shape)?;
38+
if !list.force_multi_line {
3839
// First we try formatting on one line.
39-
rewrite_pairs_one_line(&list, shape, context)
40-
.or_else(|| rewrite_pairs_multiline(&list, shape, context))
41-
})
40+
if let Some(out) = rewrite_pairs_one_line(&list, shape, context) {
41+
return Some(out);
42+
}
43+
}
44+
rewrite_pairs_multiline(&list, shape, context)
4245
}
4346

4447
// This may return a multi-line result since we allow the last expression to go
@@ -246,6 +249,7 @@ trait FlattenPair: Rewrite + Sized {
246249
struct PairList<'a, 'b, T: Rewrite> {
247250
list: Vec<(&'b T, Option<String>)>,
248251
separators: Vec<&'a str>,
252+
force_multi_line: bool,
249253
}
250254

251255
impl FlattenPair for ast::Expr {
@@ -283,6 +287,7 @@ impl FlattenPair for ast::Expr {
283287
let mut stack = vec![];
284288
let mut list = vec![];
285289
let mut separators = vec![];
290+
let mut force_multi_line = false;
286291
let mut node = self;
287292
loop {
288293
match node.kind {
@@ -293,6 +298,9 @@ impl FlattenPair for ast::Expr {
293298
_ => {
294299
let op_len = separators.last().map_or(0, |s: &&str| s.len());
295300
let rw = default_rewrite(node, op_len, list.is_empty());
301+
// a `let` expression forces multi-line, unless it is the first expression
302+
force_multi_line |=
303+
!list.is_empty() && matches!(node.kind, ast::ExprKind::Let(..));
296304
list.push((node, rw));
297305
if let Some(pop) = stack.pop() {
298306
match pop.kind {
@@ -310,7 +318,11 @@ impl FlattenPair for ast::Expr {
310318
}
311319

312320
assert_eq!(list.len() - 1, separators.len());
313-
Some(PairList { list, separators })
321+
Some(PairList {
322+
list,
323+
separators,
324+
force_multi_line,
325+
})
314326
}
315327
}
316328

tests/cargo-fmt/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Integration tests for cargo-fmt.
22

33
use std::env;
4-
use std::process::Command;
54
use std::path::Path;
5+
use std::process::Command;
66

77
/// Run the cargo-fmt executable and return its output.
88
fn cargo_fmt(args: &[&str]) -> (String, String) {

tests/source/let_chains.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
// can be one line
3+
if let x = x && x {}
4+
// must wrap
5+
if xxx && let x = x {}
6+
7+
if aaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaa && aaaaaaaaa && let Some(x) = xxxxxxxxxxxx && aaaaaaa && let None = aaaaaaaaaa {}
8+
9+
if aaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaa || aaaaaaaaa && let Some(x) = xxxxxxxxxxxx && aaaaaaa && let None = aaaaaaaaaa {}
10+
11+
}

tests/source/match.rs

+1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ fn guards() {
292292
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
293293
if fooooooooooooooooooooo &&
294294
(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => {}
295+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if let Some(foooooooooooooo) = hiiiiiiiiiiiiiii => {}
295296
}
296297
}
297298

tests/target/let_chains.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
fn main() {
2+
// can be one line
3+
if let x = x && x {}
4+
// must wrap
5+
if xxx
6+
&& let x = x
7+
{}
8+
9+
if aaaaaaaaaaaaaaaaaaaaa
10+
&& aaaaaaaaaaaaaaa
11+
&& aaaaaaaaa
12+
&& let Some(x) = xxxxxxxxxxxx
13+
&& aaaaaaa
14+
&& let None = aaaaaaaaaa
15+
{}
16+
17+
if aaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaa
18+
|| aaaaaaaaa
19+
&& let Some(x) = xxxxxxxxxxxx
20+
&& aaaaaaa
21+
&& let None = aaaaaaaaaa
22+
{}
23+
}

tests/target/match.rs

+2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ fn guards() {
317317
if fooooooooooooooooooooo
318318
&& (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
319319
|| cccccccccccccccccccccccccccccccccccccccc) => {}
320+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
321+
if let Some(foooooooooooooo) = hiiiiiiiiiiiiiii => {}
320322
}
321323
}
322324

0 commit comments

Comments
 (0)