Skip to content

Commit ee329d3

Browse files
authored
Allow bool literals as first item of a single-line let chain (#6492)
1 parent c6c8159 commit ee329d3

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

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;
@@ -266,13 +266,17 @@ struct PairList<'a, 'b, T: Rewrite> {
266266
span: Span,
267267
}
268268

269-
fn is_ident(expr: &ast::Expr) -> bool {
269+
fn is_ident_or_bool_lit(expr: &ast::Expr) -> bool {
270270
match &expr.kind {
271271
ast::ExprKind::Path(None, path) if path.segments.len() == 1 => true,
272+
ast::ExprKind::Lit(token::Lit {
273+
kind: token::LitKind::Bool,
274+
..
275+
}) => true,
272276
ast::ExprKind::Unary(_, expr)
273277
| ast::ExprKind::AddrOf(_, _, expr)
274278
| ast::ExprKind::Paren(expr)
275-
| ast::ExprKind::Try(expr) => is_ident(expr),
279+
| ast::ExprKind::Try(expr) => is_ident_or_bool_lit(expr),
276280
_ => false,
277281
}
278282
}
@@ -290,10 +294,10 @@ impl<'a, 'b> PairList<'a, 'b, ast::Expr> {
290294
return false;
291295
}
292296

293-
let fist_item_is_ident = is_ident(self.list[0].0);
297+
let fist_item_is_ident_or_bool_lit = is_ident_or_bool_lit(self.list[0].0);
294298
let second_item_is_let_chain = matches!(self.list[1].0.kind, ast::ExprKind::Let(..));
295299

296-
fist_item_is_ident && second_item_is_let_chain
300+
fist_item_is_ident_or_bool_lit && second_item_is_let_chain
297301
}
298302
}
299303

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

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)