Skip to content

Commit 6dca4f2

Browse files
committed
Add some doc for search_is_some lint
1 parent ddcbac3 commit 6dca4f2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

clippy_lints/src/methods/search_is_some.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub(super) fn check<'tcx>(
5050
if let hir::PatKind::Ref(..) = closure_arg.pat.kind {
5151
Some(search_snippet.replacen('&', "", 1))
5252
} else if let PatKind::Binding(..) = strip_pat_refs(closure_arg.pat).kind {
53+
// `find()` provides a reference to the item, but `any` does not,
54+
// so we should fix item usages for suggestion
5355
get_closure_suggestion(cx, search_arg, closure_body)
5456
.or_else(|| Some(search_snippet.to_string()))
5557
} else {
@@ -151,6 +153,9 @@ pub(super) fn check<'tcx>(
151153
}
152154
}
153155

156+
// Build suggestion gradually by handling closure arg specific usages,
157+
// such as explicit deref and borrowing cases.
158+
// Returns `None` if no such use cases have been triggered in closure body
154159
fn get_closure_suggestion<'tcx>(
155160
cx: &LateContext<'_>,
156161
search_arg: &'tcx hir::Expr<'_>,
@@ -203,8 +208,12 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
203208
let end_snip = snippet(self.cx, end_span, "..");
204209

205210
if cmt.place.projections.is_empty() {
211+
// handle item without any projection, that needs an explicit borrowing
212+
// i.e.: suggest `&x` instead of `x`
206213
self.suggestion_start.push_str(&format!("{}&{}", start_snip, ident_str));
207214
} else {
215+
// cases where a parent call is using the item
216+
// i.e.: suggest `.contains(&x)` for `.find(|x| [1, 2, 3].contains(x)).is_none()`
208217
let parent_expr = get_parent_expr_for_hir(self.cx, cmt.hir_id);
209218
if let Some(Expr { hir_id: _, kind, .. }) = parent_expr {
210219
if let ExprKind::Call(_, args) | ExprKind::MethodCall(_, _, args, _) = kind {
@@ -230,6 +239,8 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
230239
}
231240
}
232241

242+
// handle item projections by removing one explicit deref
243+
// i.e.: suggest `*x` instead of `**x`
233244
let mut replacement_str = ident_str;
234245
let last_deref = cmt
235246
.place

0 commit comments

Comments
 (0)