Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4033 search_is_some #4049

Merged
merged 5 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,19 @@ fn lint_search_is_some<'a, 'tcx>(
);
let search_snippet = snippet(cx, search_args[1].span, "..");
if search_snippet.lines().count() <= 1 {
// suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
let any_search_snippet =
if_chain! {
if search_method == "find";
if let hir::ExprKind::Closure(_, _, body_id, ..) = search_args[1].node;
flip1995 marked this conversation as resolved.
Show resolved Hide resolved
let closure_body = cx.tcx.hir().body(body_id);
if let hir::PatKind::Ref(..) = closure_body.arguments[0].pat.node;
flip1995 marked this conversation as resolved.
Show resolved Hide resolved
then {
Some(search_snippet.replacen('&', "", 1))
} else {
None
}
};
// add note if not multi-line
span_note_and_lint(
cx,
Expand All @@ -1983,8 +1996,9 @@ fn lint_search_is_some<'a, 'tcx>(
&msg,
expr.span,
&format!(
"replace `{0}({1}).is_some()` with `any({1})`",
search_method, search_snippet
"replace `{0}({1}).is_some()` with `any({2})`",
search_method, search_snippet,
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
),
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/methods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::search-is-some` implied by `-D warnings`
= note: replace `find(|&x| *x < 0).is_some()` with `any(|&x| *x < 0)`
= note: replace `find(|&x| *x < 0).is_some()` with `any(|x| *x < 0)`

error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:236:13
Expand Down