diff options
| author | Laurențiu Nicola <lnicola@users.noreply.github.com> | 2025-05-01 07:33:30 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-01 07:33:30 +0000 |
| commit | 00d2f60efd516bc7ea658bd0a6de5e2f1f1df322 (patch) | |
| tree | 9375885343e27ab26b20dfe5a3714e00d5d378d0 /src/tools/rustfmt | |
| parent | e7502210ce5ab2d92cb87c372dab51b637ba6df4 (diff) | |
| parent | 1c5de64814d72effc6890ca823fa4d248041a0bd (diff) | |
| download | rust-00d2f60efd516bc7ea658bd0a6de5e2f1f1df322.tar.gz rust-00d2f60efd516bc7ea658bd0a6de5e2f1f1df322.zip | |
Merge pull request #19726 from lnicola/sync-from-rust
Sync from downstream again
Diffstat (limited to 'src/tools/rustfmt')
| -rw-r--r-- | src/tools/rustfmt/src/pairs.rs | 14 | ||||
| -rw-r--r-- | src/tools/rustfmt/src/types.rs | 13 | ||||
| -rw-r--r-- | src/tools/rustfmt/tests/source/let_chains.rs | 10 | ||||
| -rw-r--r-- | src/tools/rustfmt/tests/target/let_chains.rs | 8 |
4 files changed, 30 insertions, 15 deletions
diff --git a/src/tools/rustfmt/src/pairs.rs b/src/tools/rustfmt/src/pairs.rs index 9c51298416b..17ff041d775 100644 --- a/src/tools/rustfmt/src/pairs.rs +++ b/src/tools/rustfmt/src/pairs.rs @@ -1,4 +1,4 @@ -use rustc_ast::ast; +use rustc_ast::{ast, token}; use rustc_span::Span; use crate::config::IndentStyle; @@ -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, } } @@ -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 } } diff --git a/src/tools/rustfmt/src/types.rs b/src/tools/rustfmt/src/types.rs index 75a5a8532b8..7ec1032dcb4 100644 --- a/src/tools/rustfmt/src/types.rs +++ b/src/tools/rustfmt/src/types.rs @@ -1093,6 +1093,19 @@ impl Rewrite for ast::TyPat { ast::TyPatKind::Range(ref lhs, ref rhs, ref end_kind) => { rewrite_range_pat(context, shape, lhs, rhs, end_kind, self.span) } + ast::TyPatKind::Or(ref variants) => { + let mut first = true; + let mut s = String::new(); + for variant in variants { + if first { + first = false + } else { + s.push_str(" | "); + } + s.push_str(&variant.rewrite_result(context, shape)?); + } + Ok(s) + } ast::TyPatKind::Err(_) => Err(RewriteError::Unknown), } } diff --git a/src/tools/rustfmt/tests/source/let_chains.rs b/src/tools/rustfmt/tests/source/let_chains.rs index b7c1f811096..0c4d8aa85ea 100644 --- a/src/tools/rustfmt/tests/source/let_chains.rs +++ b/src/tools/rustfmt/tests/source/let_chains.rs @@ -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 @@ -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 { diff --git a/src/tools/rustfmt/tests/target/let_chains.rs b/src/tools/rustfmt/tests/target/let_chains.rs index 1ceecac8abc..204937b4cac 100644 --- a/src/tools/rustfmt/tests/target/let_chains.rs +++ b/src/tools/rustfmt/tests/target/let_chains.rs @@ -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 {}; @@ -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 |
