Skip to content

Commit

Permalink
make redundant_local's find_binding more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
max-niederman committed Jun 19, 2023
1 parent c3d756f commit 188f153
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions clippy_lints/src/redundant_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,15 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocal {

/// Find the annotation of a binding introduced by a pattern, or `None` if it's not introduced.
fn find_binding(pat: &Pat<'_>, name: Ident) -> Option<BindingAnnotation> {
match pat.kind {
PatKind::Binding(annotation, _, ident, _) if ident == name => Some(annotation),
PatKind::Binding(_, _, _, Some(subpat)) => find_binding(subpat, name),
PatKind::Struct(_, fields, _) => fields.iter().find_map(|field| find_binding(field.pat, name)),
PatKind::TupleStruct(_, fields, _) | PatKind::Or(fields) | PatKind::Tuple(fields, _) => {
fields.iter().find_map(|field| find_binding(field, name))
},
PatKind::Slice(before, middle, after) => before
.iter()
.chain(middle)
.chain(after.iter())
.find_map(|field| find_binding(field, name)),
PatKind::Box(inner) | PatKind::Ref(inner, _) => find_binding(inner, name),
PatKind::Wild
| PatKind::Binding(_, _, _, None)
| PatKind::Path(_)
| PatKind::Lit(_)
| PatKind::Range(_, _, _) => None,
}
let mut ret = None;

pat.each_binding_or_first(&mut |annotation, _, _, ident| {
if ident == name {
ret = Some(annotation);
}
});

ret
}

/// Check if a rebinding of a local affects the code's drop behavior.
Expand Down

0 comments on commit 188f153

Please sign in to comment.