Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5df286b

Browse files
committed
Improve SpanlessEq for blocks
1 parent 68cf94f commit 5df286b

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

clippy_lints/src/utils/hir_utils.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
8181
}
8282
}
8383

84-
match (&left.kind, &right.kind) {
84+
match (&reduce_exprkind(&left.kind), &reduce_exprkind(&right.kind)) {
8585
(&ExprKind::AddrOf(lb, l_mut, ref le), &ExprKind::AddrOf(rb, r_mut, ref re)) => {
8686
lb == rb && l_mut == r_mut && self.eq_expr(le, re)
8787
},
@@ -306,6 +306,32 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
306306
}
307307
}
308308

309+
/// Some simple reductions like `{ return }` => `return`
310+
fn reduce_exprkind<'hir>(kind: &'hir ExprKind<'hir>) -> &ExprKind<'hir> {
311+
if let ExprKind::Block(block, _) = kind {
312+
match (block.stmts, block.expr) {
313+
// `{}` => `()`
314+
([], None) => &ExprKind::Tup(&[]),
315+
([], Some(expr)) => match expr.kind {
316+
// `{ return .. }` => `return ..`
317+
ExprKind::Ret(..) => &expr.kind,
318+
_ => kind,
319+
},
320+
([stmt], None) => match stmt.kind {
321+
StmtKind::Expr(expr) | StmtKind::Semi(expr) => match expr.kind {
322+
// `{ return ..; }` => `return ..`
323+
ExprKind::Ret(..) => &expr.kind,
324+
_ => kind,
325+
},
326+
_ => kind,
327+
},
328+
_ => kind,
329+
}
330+
} else {
331+
kind
332+
}
333+
}
334+
309335
fn swap_binop<'a>(
310336
binop: BinOpKind,
311337
lhs: &'a Expr<'a>,

0 commit comments

Comments
 (0)