Skip to content

rustfmt: Also allow bool literals as first item of let chain #140486

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 9 additions & 5 deletions src/tools/rustfmt/src/pairs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_ast::ast;
use rustc_ast::{ast, token};
use rustc_span::Span;

use crate::config::IndentStyle;
Expand Down Expand Up @@ -272,13 +272,17 @@ struct PairList<'a, 'b, T: Rewrite> {
span: Span,
}

fn is_ident(expr: &ast::Expr) -> bool {
fn is_ident_or_bool_lit(expr: &ast::Expr) -> bool {
match &expr.kind {
ast::ExprKind::Path(None, path) if path.segments.len() == 1 => true,
ast::ExprKind::Lit(token::Lit {
kind: token::LitKind::Bool,
..
}) => true,
ast::ExprKind::Unary(_, expr)
| ast::ExprKind::AddrOf(_, _, expr)
| ast::ExprKind::Paren(expr)
| ast::ExprKind::Try(expr) => is_ident(expr),
| ast::ExprKind::Try(expr) => is_ident_or_bool_lit(expr),
_ => false,
}
}
Expand All @@ -296,10 +300,10 @@ impl<'a, 'b> PairList<'a, 'b, ast::Expr> {
return false;
}

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

fist_item_is_ident && second_item_is_let_chain
fist_item_is_ident_or_bool_lit && second_item_is_let_chain
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/tools/rustfmt/tests/source/let_chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ fn test_single_line_let_chain() {
if a && let Some(b) = foo() {
}

// first item in let-chain is a bool literal
if true && let Some(x) = y {

}

// first item in let-chain is a unary ! with an ident
let unary_not = if !from_hir_call
&& let Some(p) = parent
Expand Down Expand Up @@ -94,11 +99,6 @@ fn test_multi_line_let_chain() {

}

// bool literal
if true && let Some(x) = y {

}

// cast to a bool
if 1 as bool && let Some(x) = y {

Expand Down
8 changes: 3 additions & 5 deletions src/tools/rustfmt/tests/target/let_chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ fn test_single_line_let_chain() {
// first item in let-chain is an ident
if a && let Some(b) = foo() {}

// first item in let-chain is a bool literal
if true && let Some(x) = y {}

// first item in let-chain is a unary ! with an ident
let unary_not = if !from_hir_call && let Some(p) = parent {};

Expand Down Expand Up @@ -102,11 +105,6 @@ fn test_multi_line_let_chain() {
&& let Some(x) = y
{}

// bool literal
if true
&& let Some(x) = y
{}

// cast to a bool
if 1 as bool
&& let Some(x) = y
Expand Down
Loading