Skip to content

Commit f39d18b

Browse files
committed
Auto merge of rust-lang#118527 - Nadrieril:never_patterns_parse, r=compiler-errors
never_patterns: Parse match arms with no body Never patterns are meant to signal unreachable cases, and thus don't take bodies: ```rust let ptr: *const Option<!> = ...; match *ptr { None => { foo(); } Some(!), } ``` This PR makes rustc accept the above, and enforces that an arm has a body xor is a never pattern. This affects parsing of match arms even with the feature off, so this is delicate. (Plus this is my first non-trivial change to the parser). ~~The last commit is optional; it introduces a bit of churn to allow the new suggestions to be machine-applicable. There may be a better solution? I'm not sure.~~ EDIT: I removed that commit r? `@compiler-errors`
2 parents 5f191ce + 7ffe1ff commit f39d18b

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

clippy_lints/src/non_expressive_names.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> {
341341

342342
self.apply(|this| {
343343
SimilarNamesNameVisitor(this).visit_pat(&arm.pat);
344-
this.apply(|this| walk_expr(this, &arm.body));
344+
if let Some(body) = &arm.body {
345+
this.apply(|this| walk_expr(this, body));
346+
}
345347
});
346348

347349
self.check_single_char_names();

clippy_lints/src/redundant_else.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ impl<'ast> Visitor<'ast> for BreakVisitor {
105105
fn visit_expr(&mut self, expr: &'ast Expr) {
106106
self.is_break = match expr.kind {
107107
ExprKind::Break(..) | ExprKind::Continue(..) | ExprKind::Ret(..) => true,
108-
ExprKind::Match(_, ref arms) => arms.iter().all(|arm| self.check_expr(&arm.body)),
108+
ExprKind::Match(_, ref arms) => arms.iter().all(|arm|
109+
arm.body.is_none() || arm.body.as_deref().is_some_and(|body| self.check_expr(body))
110+
),
109111
ExprKind::If(_, ref then, Some(ref els)) => self.check_block(then) && self.check_expr(els),
110112
ExprKind::If(_, _, None)
111113
// ignore loops for simplicity

clippy_utils/src/ast_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub fn eq_field(l: &ExprField, r: &ExprField) -> bool {
236236
pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
237237
l.is_placeholder == r.is_placeholder
238238
&& eq_pat(&l.pat, &r.pat)
239-
&& eq_expr(&l.body, &r.body)
239+
&& eq_expr_opt(&l.body, &r.body)
240240
&& eq_expr_opt(&l.guard, &r.guard)
241241
&& over(&l.attrs, &r.attrs, eq_attr)
242242
}

0 commit comments

Comments
 (0)