Skip to content

Commit

Permalink
Fix clippy for let-else
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed Aug 31, 2021
1 parent 960ea09 commit 04d7903
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/tools/clippy/clippy_lints/src/non_expressive_names.rs
Expand Up @@ -316,8 +316,11 @@ impl<'a, 'b> SimilarNamesLocalVisitor<'a, 'b> {

impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> {
fn visit_local(&mut self, local: &'tcx Local) {
if let Some(ref init) = local.init {
self.apply(|this| walk_expr(this, &**init));
if let Some((init, els)) = &local.kind.init_else_opt() {
self.apply(|this| walk_expr(this, init));
if let Some(els) = els {
self.apply(|this| walk_block(this, els));
}
}
// add the pattern after the expression because the bindings aren't available
// yet in the init
Expand Down
12 changes: 11 additions & 1 deletion src/tools/clippy/clippy_utils/src/ast_utils.rs
Expand Up @@ -221,7 +221,7 @@ pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
(Local(l), Local(r)) => {
eq_pat(&l.pat, &r.pat)
&& both(&l.ty, &r.ty, |l, r| eq_ty(l, r))
&& eq_expr_opt(&l.init, &r.init)
&& eq_local_kind(&l.kind, &r.kind)
&& over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
},
(Item(l), Item(r)) => eq_item(l, r, eq_item_kind),
Expand All @@ -234,6 +234,16 @@ pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
}
}

pub fn eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool {
use LocalKind::*;
match (l, r) {
(Decl, Decl) => true,
(Init(l), Init(r)) => eq_expr(l, r),
(InitElse(li, le), InitElse(ri, re)) => eq_expr(li, ri) && eq_block(le, re),
_ => false,
}
}

pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
eq_id(l.ident, r.ident)
&& over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
Expand Down

0 comments on commit 04d7903

Please sign in to comment.