Skip to content

Commit 04300d8

Browse files
Also allow bool literals as first item of let chain
Co-authored-by: est31 <[email protected]>
1 parent 7450913 commit 04300d8

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

src/tools/rustfmt/src/pairs.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::ast;
1+
use rustc_ast::{ast, token};
22
use rustc_span::Span;
33

44
use crate::config::IndentStyle;
@@ -272,13 +272,17 @@ struct PairList<'a, 'b, T: Rewrite> {
272272
span: Span,
273273
}
274274

275-
fn is_ident(expr: &ast::Expr) -> bool {
275+
fn is_ident_or_bool_lit(expr: &ast::Expr) -> bool {
276276
match &expr.kind {
277277
ast::ExprKind::Path(None, path) if path.segments.len() == 1 => true,
278+
ast::ExprKind::Lit(token::Lit {
279+
kind: token::LitKind::Bool,
280+
..
281+
}) => true,
278282
ast::ExprKind::Unary(_, expr)
279283
| ast::ExprKind::AddrOf(_, _, expr)
280284
| ast::ExprKind::Paren(expr)
281-
| ast::ExprKind::Try(expr) => is_ident(expr),
285+
| ast::ExprKind::Try(expr) => is_ident_or_bool_lit(expr),
282286
_ => false,
283287
}
284288
}
@@ -296,10 +300,10 @@ impl<'a, 'b> PairList<'a, 'b, ast::Expr> {
296300
return false;
297301
}
298302

299-
let fist_item_is_ident = is_ident(self.list[0].0);
303+
let fist_item_is_ident_or_bool_lit = is_ident_or_bool_lit(self.list[0].0);
300304
let second_item_is_let_chain = matches!(self.list[1].0.kind, ast::ExprKind::Let(..));
301305

302-
fist_item_is_ident && second_item_is_let_chain
306+
fist_item_is_ident_or_bool_lit && second_item_is_let_chain
303307
}
304308
}
305309

src/tools/rustfmt/tests/source/let_chains.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ fn test_single_line_let_chain() {
2020
if a && let Some(b) = foo() {
2121
}
2222

23+
// first item in let-chain is a bool literal
24+
if true && let Some(x) = y {
25+
26+
}
27+
2328
// first item in let-chain is a unary ! with an ident
2429
let unary_not = if !from_hir_call
2530
&& let Some(p) = parent
@@ -94,11 +99,6 @@ fn test_multi_line_let_chain() {
9499

95100
}
96101

97-
// bool literal
98-
if true && let Some(x) = y {
99-
100-
}
101-
102102
// cast to a bool
103103
if 1 as bool && let Some(x) = y {
104104

src/tools/rustfmt/tests/target/let_chains.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ fn test_single_line_let_chain() {
5050
// first item in let-chain is an ident
5151
if a && let Some(b) = foo() {}
5252

53+
// first item in let-chain is a bool literal
54+
if true && let Some(x) = y {}
55+
5356
// first item in let-chain is a unary ! with an ident
5457
let unary_not = if !from_hir_call && let Some(p) = parent {};
5558

@@ -102,11 +105,6 @@ fn test_multi_line_let_chain() {
102105
&& let Some(x) = y
103106
{}
104107

105-
// bool literal
106-
if true
107-
&& let Some(x) = y
108-
{}
109-
110108
// cast to a bool
111109
if 1 as bool
112110
&& let Some(x) = y

0 commit comments

Comments
 (0)