refactor: extract common pat_is_wild to clippy_utils

This function was previously defined for the iter_kv_map,
for_kw_map, and unused_enumerate_index lints. This commit extracts
it into clippy_utils.
This commit is contained in:
Dinu Blanovschi 2023-11-01 14:10:42 +01:00
parent 14b82909b0
commit bb9cc6d47c
4 changed files with 15 additions and 34 deletions

View file

@ -2960,3 +2960,15 @@ op_utils! {
Shl ShlAssign
Shr ShrAssign
}
/// Returns `true` if the pattern is a `PatWild`, or is an ident prefixed with `_`
/// that is not locally used.
pub fn pat_is_wild<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx PatKind<'_>, body: impl Visitable<'tcx>) -> bool {
match *pat {
PatKind::Wild => true,
PatKind::Binding(_, id, ident, None) if ident.as_str().starts_with('_') => {
!visitors::is_local_used(cx, body, id)
},
_ => false,
}
}