Skip to content

Commit 286e492

Browse files
authored
Rollup merge of #79051 - LeSeulArtichaut:if-let-guard, r=matthewjasper
Implement if-let match guards Implements rust-lang/rfcs#2294 (tracking issue: #51114). I probably should do a few more things before this can be merged: - [x] Add tests (added basic tests, more advanced tests could be done in the future?) - [x] Add lint for exhaustive if-let guard (comparable to normal if-let statements) - [x] Fix clippy However since this is a nightly feature maybe it's fine to land this and do those steps in follow-up PRs. Thanks a lot `@matthewjasper` ❤️ for helping me with lowering to MIR! Would you be interested in reviewing this? r? `@ghost` for now
2 parents 39aca5f + 93c6135 commit 286e492

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

clippy_lints/src/shadow.rs

+4
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ fn check_expr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, bindings: &mut
342342
if let Some(ref guard) = arm.guard {
343343
match guard {
344344
Guard::If(if_expr) => check_expr(cx, if_expr, bindings),
345+
Guard::IfLet(guard_pat, guard_expr) => {
346+
check_pat(cx, guard_pat, Some(*guard_expr), guard_pat.span, bindings);
347+
check_expr(cx, guard_expr, bindings);
348+
},
345349
}
346350
}
347351
check_expr(cx, &arm.body, bindings);

clippy_lints/src/utils/author.rs

+13
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
372372
self.current = if_expr_pat;
373373
self.visit_expr(if_expr);
374374
},
375+
hir::Guard::IfLet(ref if_let_pat, ref if_let_expr) => {
376+
let if_let_pat_pat = self.next("pat");
377+
let if_let_expr_pat = self.next("expr");
378+
println!(
379+
" if let Guard::IfLet(ref {}, ref {}) = {};",
380+
if_let_pat_pat, if_let_expr_pat, guard_pat
381+
);
382+
self.current = if_let_expr_pat;
383+
self.visit_expr(if_let_expr);
384+
self.current = if_let_pat_pat;
385+
self.visit_pat(if_let_pat);
386+
},
375387
}
376388
}
377389
self.current = format!("{}[{}].pat", arms_pat, i);
@@ -730,6 +742,7 @@ fn desugaring_name(des: hir::MatchSource) -> String {
730742
"MatchSource::IfLetDesugar {{ contains_else_clause: {} }}",
731743
contains_else_clause
732744
),
745+
hir::MatchSource::IfLetGuardDesugar => "MatchSource::IfLetGuardDesugar".to_string(),
733746
hir::MatchSource::IfDesugar { contains_else_clause } => format!(
734747
"MatchSource::IfDesugar {{ contains_else_clause: {} }}",
735748
contains_else_clause

clippy_lints/src/utils/hir_utils.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
169169
fn eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool {
170170
match (left, right) {
171171
(Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
172+
(Guard::IfLet(lp, le), Guard::IfLet(rp, re)) => self.eq_pat(lp, rp) && self.eq_expr(le, re),
173+
_ => false,
172174
}
173175
}
174176

@@ -669,7 +671,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
669671

670672
pub fn hash_guard(&mut self, g: &Guard<'_>) {
671673
match g {
672-
Guard::If(ref expr) => {
674+
Guard::If(ref expr) | Guard::IfLet(_, ref expr) => {
673675
self.hash_expr(expr);
674676
},
675677
}

clippy_lints/src/utils/inspector.rs

+5
Original file line numberDiff line numberDiff line change
@@ -560,5 +560,10 @@ fn print_guard(cx: &LateContext<'_>, guard: &hir::Guard<'_>, indent: usize) {
560560
println!("{}If", ind);
561561
print_expr(cx, expr, indent + 1);
562562
},
563+
hir::Guard::IfLet(pat, expr) => {
564+
println!("{}IfLet", ind);
565+
print_pat(cx, pat, indent + 1);
566+
print_expr(cx, expr, indent + 1);
567+
},
563568
}
564569
}

0 commit comments

Comments
 (0)