From 2d4d275519e6dc3d2f5ffca0164988c6876c0386 Mon Sep 17 00:00:00 2001 From: airt Date: Wed, 1 May 2019 01:06:15 +0800 Subject: [PATCH 1/5] change |&x| to |x| in stderr file --- tests/ui/methods.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/methods.stderr b/tests/ui/methods.stderr index ba93be5f4623..4bf53acf01ef 100644 --- a/tests/ui/methods.stderr +++ b/tests/ui/methods.stderr @@ -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 From bd0b75f6c3a8c147aa95a64877280abf1fc1b069 Mon Sep 17 00:00:00 2001 From: airt Date: Wed, 1 May 2019 01:08:16 +0800 Subject: [PATCH 2/5] fix suggestion for search_is_some naively --- clippy_lints/src/methods/mod.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index d8595ea9004c..0b3a406bd760 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1975,6 +1975,13 @@ 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 search_method == "find" && search_snippet.starts_with("|&") { + Some(search_snippet.replacen('&', "", 1)) + } else { + None + }; // add note if not multi-line span_note_and_lint( cx, @@ -1983,8 +1990,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 { From c3fde34fd53a6eed89dd30064b1985acf39f7f07 Mon Sep 17 00:00:00 2001 From: airt Date: Wed, 1 May 2019 03:09:27 +0800 Subject: [PATCH 3/5] fix suggestion for search_is_some --- clippy_lints/src/methods/mod.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 0b3a406bd760..83f7d2a1f9ea 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1977,10 +1977,16 @@ fn lint_search_is_some<'a, 'tcx>( if search_snippet.lines().count() <= 1 { // suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()` let any_search_snippet = - if search_method == "find" && search_snippet.starts_with("|&") { - Some(search_snippet.replacen('&', "", 1)) - } else { - None + if_chain! { + if search_method == "find"; + if let hir::ExprKind::Closure(_, _, body_id, ..) = search_args[1].node; + let closure_body = cx.tcx.hir().body(body_id); + if let hir::PatKind::Ref(..) = closure_body.arguments[0].pat.node; + then { + Some(search_snippet.replacen('&', "", 1)) + } else { + None + } }; // add note if not multi-line span_note_and_lint( From 5d6a100f8112f1cc0dadbb34220f1b8a849cdfbe Mon Sep 17 00:00:00 2001 From: airt Date: Wed, 1 May 2019 04:03:51 +0800 Subject: [PATCH 4/5] format code --- clippy_lints/src/methods/mod.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 83f7d2a1f9ea..07a207545a0d 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1976,18 +1976,17 @@ 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; - let closure_body = cx.tcx.hir().body(body_id); - if let hir::PatKind::Ref(..) = closure_body.arguments[0].pat.node; - then { - Some(search_snippet.replacen('&', "", 1)) - } else { - None - } - }; + let any_search_snippet = if_chain! { + if search_method == "find"; + if let hir::ExprKind::Closure(_, _, body_id, ..) = search_args[1].node; + let closure_body = cx.tcx.hir().body(body_id); + if let hir::PatKind::Ref(..) = closure_body.arguments[0].pat.node; + then { + Some(search_snippet.replacen('&', "", 1)) + } else { + None + } + }; // add note if not multi-line span_note_and_lint( cx, @@ -1997,7 +1996,8 @@ fn lint_search_is_some<'a, 'tcx>( expr.span, &format!( "replace `{0}({1}).is_some()` with `any({2})`", - search_method, search_snippet, + search_method, + search_snippet, any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str) ), ); From d063516c85a2d0ae368911bd8ffe3a9459a252ba Mon Sep 17 00:00:00 2001 From: airt Date: Wed, 1 May 2019 04:41:00 +0800 Subject: [PATCH 5/5] check closure arguments before use it --- clippy_lints/src/methods/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 07a207545a0d..3ea18f1d14d4 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1980,7 +1980,8 @@ fn lint_search_is_some<'a, 'tcx>( if search_method == "find"; if let hir::ExprKind::Closure(_, _, body_id, ..) = search_args[1].node; let closure_body = cx.tcx.hir().body(body_id); - if let hir::PatKind::Ref(..) = closure_body.arguments[0].pat.node; + if let Some(closure_arg) = closure_body.arguments.get(0); + if let hir::PatKind::Ref(..) = closure_arg.pat.node; then { Some(search_snippet.replacen('&', "", 1)) } else {