Skip to content

Commit bb9cc6d

Browse files
committed
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.
1 parent 14b8290 commit bb9cc6d

File tree

4 files changed

+15
-34
lines changed

4 files changed

+15
-34
lines changed

clippy_lints/src/loops/for_kv_map.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use super::FOR_KV_MAP;
22
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
33
use clippy_utils::source::snippet;
4-
use clippy_utils::sugg;
54
use clippy_utils::ty::is_type_diagnostic_item;
6-
use clippy_utils::visitors::is_local_used;
5+
use clippy_utils::{pat_is_wild, sugg};
76
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Pat, PatKind};
87
use rustc_lint::LateContext;
98
use rustc_middle::ty;
@@ -55,12 +54,3 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx
5554
}
5655
}
5756
}
58-
59-
/// Returns `true` if the pattern is a `PatWild` or an ident prefixed with `_`.
60-
fn pat_is_wild<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx PatKind<'_>, body: &'tcx Expr<'_>) -> bool {
61-
match *pat {
62-
PatKind::Wild => true,
63-
PatKind::Binding(_, id, ident, None) if ident.as_str().starts_with('_') => !is_local_used(cx, body, id),
64-
_ => false,
65-
}
66-
}

clippy_lints/src/loops/unused_enumerate_index.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use super::UNUSED_ENUMERATE_INDEX;
22
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
33
use clippy_utils::source::snippet;
4-
use clippy_utils::sugg;
5-
use clippy_utils::visitors::is_local_used;
4+
use clippy_utils::{pat_is_wild, sugg};
65
use rustc_hir::def::DefKind;
76
use rustc_hir::{Expr, ExprKind, Pat, PatKind};
87
use rustc_lint::LateContext;
@@ -61,12 +60,3 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx
6160
},
6261
);
6362
}
64-
65-
/// Returns `true` if the pattern is a `PatWild` or an ident prefixed with `_`.
66-
fn pat_is_wild<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx PatKind<'_>, body: &'tcx Expr<'_>) -> bool {
67-
match *pat {
68-
PatKind::Wild => true,
69-
PatKind::Binding(_, id, ident, None) if ident.as_str().starts_with('_') => !is_local_used(cx, body, id),
70-
_ => false,
71-
}
72-
}

clippy_lints/src/methods/iter_kv_map.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
use super::ITER_KV_MAP;
44
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_sugg, span_lint_and_then};
55
use clippy_utils::source::{snippet, snippet_with_applicability};
6-
use clippy_utils::sugg;
76
use clippy_utils::ty::is_type_diagnostic_item;
8-
use clippy_utils::visitors::is_local_used;
7+
use clippy_utils::{pat_is_wild, sugg};
98
use rustc_hir::{BindingAnnotation, Body, BorrowKind, ByRef, Expr, ExprKind, Mutability, Pat, PatKind};
109
use rustc_lint::{LateContext, LintContext};
1110
use rustc_middle::ty;
@@ -84,13 +83,3 @@ pub(super) fn check<'tcx>(
8483
}
8584
}
8685
}
87-
88-
/// Returns `true` if the pattern is a `PatWild`, or is an ident prefixed with `_`
89-
/// that is not locally used.
90-
fn pat_is_wild<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx PatKind<'_>, body: &'tcx Expr<'_>) -> bool {
91-
match *pat {
92-
PatKind::Wild => true,
93-
PatKind::Binding(_, id, ident, None) if ident.as_str().starts_with('_') => !is_local_used(cx, body, id),
94-
_ => false,
95-
}
96-
}

clippy_utils/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,3 +2960,15 @@ op_utils! {
29602960
Shl ShlAssign
29612961
Shr ShrAssign
29622962
}
2963+
2964+
/// Returns `true` if the pattern is a `PatWild`, or is an ident prefixed with `_`
2965+
/// that is not locally used.
2966+
pub fn pat_is_wild<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx PatKind<'_>, body: impl Visitable<'tcx>) -> bool {
2967+
match *pat {
2968+
PatKind::Wild => true,
2969+
PatKind::Binding(_, id, ident, None) if ident.as_str().starts_with('_') => {
2970+
!visitors::is_local_used(cx, body, id)
2971+
},
2972+
_ => false,
2973+
}
2974+
}

0 commit comments

Comments
 (0)