10000 Support let-chains by ytmimi · Pull Request #5910 · rust-lang/rustfmt · GitHub
[go: up one dir, main page]

Skip to content

Support let-chains #5910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add support for ExprKind::Let
  • Loading branch information
camsteffen authored and ytmimi committed Oct 11, 2023
commit 0bb8df889ef2d318b8898f4d91a975821092b297
33 changes: 32 additions & 1 deletion src/expr.rs
8000
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub(crate) fn format_expr(
ast::ExprKind::Tup(ref items) => {
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
}
ast::ExprKind::Let(..) => None,
ast::ExprKind::Let(ref pat, ref expr, _span) => rewrite_let(context, shape, pat, expr),
ast::ExprKind::If(..)
| ast::ExprKind::ForLoop(..)
| ast::ExprKind::Loop(..)
Expand Down Expand Up @@ -1834,6 +1834,37 @@ fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>(
Some(format!("({list_str})"))
}

fn rewrite_let(
context: &RewriteContext<'_>,
shape: Shape,
pat: &ast::Pat,
expr: &ast::Expr,
) -> Option<String> {
let mut result = "let ".to_owned();

// 4 = "let ".len()
let pat_shape = shape.offset_left(4)?;
let pat_str = pat.rewrite(context, pat_shape)?;
result.push_str(&pat_str);

result.push_str(" =");

let comments_lo = context
.snippet_provider
.span_after(expr.span.with_lo(pat.span.hi()), "=");
let comments_span = mk_sp(comments_lo, expr.span.lo());
rewrite_assign_rhs_with_comments(
context,
result,
expr,
shape,
&RhsAssignKind::Expr(&expr.kind, expr.span),
RhsTactics::Default,
comments_span,
true,
)
}

pub(crate) fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
context: &'a RewriteContext<'_>,
items: impl Iterator<Item = &'a T>,
Expand Down
16 changes: 16 additions & 0 deletions tests/source/let_chains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn main() {
if let x = x && x {}

if xxx && let x = x {}

if aaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaa && aaaaaaaaa && let Some(x) = xxxxxxxxxxxx && aaaaaaa && let None = aaaaaaaaaa {}

if aaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaa || aaaaaaaaa && let Some(x) = xxxxxxxxxxxx && aaaaaaa && let None = aaaaaaaaaa {}

if let Some(Struct { x:TS(1,2) }) = path::to::<_>(hehe)
&& let [Simple, people] = /* get ready */ create_universe(/* hi */ GreatPowers).initialize_badminton().populate_swamps() &&
let everybody = (Loops { hi /*hi*/ , ..loopy() }) || summons::triumphantly() { todo!() }

if let XXXXXXXXX { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyy, zzzzzzzzzzzzz} = xxxxxxx()
&& let Foo = bar() { todo!() }
}
3 changes: 3 additions & 0 deletions tests/source/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ fn guards() {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
if fooooooooooooooooooooo &&
(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => {}
Hi { friend } if let None = friend => {}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if let Some(foooooooooooooo) = hiiiiiiiiiiiiiii => {}
aaaaaaaaaaaaaaaaa if let Superman { powers: Some(goteem), .. } = all::get_random_being::<Super>() => {}
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/target/let_chains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
fn main() {
if let x = x && x {}

if xxx && let x = x {}

if aaaaaaaaaaaaaaaaaaaaa
&& aaaaaaaaaaaaaaa
&& aaaaaaaaa
&& let Some(x) = xxxxxxxxxxxx
&& aaaaaaa
&& let None = aaaaaaaaaa
{}

if aaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaa
|| aaaaaaaaa && let Some(x) = xxxxxxxxxxxx && aaaaaaa && let None = aaaaaaaaaa
{}

if let Some(Struct { x: TS(1, 2) }) = path::to::<_>(hehe)
&& let [Simple, people] = /* get ready */
create_universe(/* hi */ GreatPowers)
.initialize_badminton()
.populate_swamps()
&& let everybody = (Loops {
hi, /*hi*/
..loopy()
})
|| summons::triumphantly()
{
todo!()
}

if let XXXXXXXXX {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
yyyyyyyyyyyyy,
zzzzzzzzzzzzz,
} = xxxxxxx()
&& let Foo = bar()
{
todo!()
}
}
8 changes: 8 additions & 0 deletions tests/target/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ fn guards() {
if fooooooooooooooooooooo
&& (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|| cccccccccccccccccccccccccccccccccccccccc) => {}
Hi { friend } if let None = friend => {}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
if let Some(foooooooooooooo) = hiiiiiiiiiiiiiii => {}
aaaaaaaaaaaaaaaaa
if let Superman {
powers: Some(goteem),
..
} = all::get_random_being::<Super>() => {}
Comment on lines +320 to +327
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this will cover #4955 too?

@calebcartwright yup, though this seems to be the only test for it (pulled from the original PR). Do we want to support if-let guard formatting with this one or should we hold off on that until a future PR?

}
}

Expand Down
0