Skip to content

Commit

Permalink
search_is_some: rename to search_is_some_or_none
Browse files Browse the repository at this point in the history
Due to enhancements added to this lint in previous commit.
  • Loading branch information
mgacek8 committed Mar 20, 2021
1 parent 21318e8 commit a65b0d5
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 65 deletions.
6 changes: 3 additions & 3 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&methods::OPTION_MAP_OR_NONE,
&methods::OR_FUN_CALL,
&methods::RESULT_MAP_OR_INTO_OPTION,
&methods::SEARCH_IS_SOME,
&methods::SEARCH_IS_SOME_OR_NONE,
&methods::SHOULD_IMPLEMENT_TRAIT,
&methods::SINGLE_CHAR_ADD_STR,
&methods::SINGLE_CHAR_PATTERN,
Expand Down Expand Up @@ -1600,7 +1600,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&methods::OPTION_MAP_OR_NONE),
LintId::of(&methods::OR_FUN_CALL),
LintId::of(&methods::RESULT_MAP_OR_INTO_OPTION),
LintId::of(&methods::SEARCH_IS_SOME),
LintId::of(&methods::SEARCH_IS_SOME_OR_NONE),
LintId::of(&methods::SHOULD_IMPLEMENT_TRAIT),
LintId::of(&methods::SINGLE_CHAR_ADD_STR),
LintId::of(&methods::SINGLE_CHAR_PATTERN),
Expand Down Expand Up @@ -1892,7 +1892,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&methods::MANUAL_FILTER_MAP),
LintId::of(&methods::MANUAL_FIND_MAP),
LintId::of(&methods::OPTION_AS_REF_DEREF),
LintId::of(&methods::SEARCH_IS_SOME),
LintId::of(&methods::SEARCH_IS_SOME_OR_NONE),
LintId::of(&methods::SKIP_WHILE_NEXT),
LintId::of(&methods::SUSPICIOUS_MAP),
LintId::of(&methods::UNNECESSARY_FILTER_MAP),
Expand Down
29 changes: 17 additions & 12 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod option_as_ref_deref;
mod option_map_or_none;
mod option_map_unwrap_or;
mod or_fun_call;
mod search_is_some;
mod search_is_some_or_none;
mod single_char_insert_string;
mod single_char_pattern;
mod single_char_push_string;
Expand Down Expand Up @@ -588,26 +588,31 @@ declare_clippy_lint! {

declare_clippy_lint! {
/// **What it does:** Checks for an iterator or string search (such as `find()`,
/// `position()`, or `rposition()`) followed by a call to `is_some()`.
/// `position()`, or `rposition()`) followed by a call to `is_some()` or `is_none()`.
///
/// **Why is this bad?** Readability, this can be written more concisely as
/// `_.any(_)` or `_.contains(_)`.
/// **Why is this bad?** Readability, this can be written more concisely as:
/// * `_.any(_)`, or `_.contains(_)` for `is_some()`,
/// * `!_.any(_)`, or `!_.contains(_)` for `is_none()`.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// # let vec = vec![1];
/// let vec = vec![1];
/// vec.iter().find(|x| **x == 0).is_some();
///
/// let _ = "hello world".find("world").is_none();
/// ```
/// Could be written as
/// ```rust
/// # let vec = vec![1];
/// let vec = vec![1];
/// vec.iter().any(|x| *x == 0);
///
/// let _ = !"hello world".contains("world");
/// ```
pub SEARCH_IS_SOME,
pub SEARCH_IS_SOME_OR_NONE,
complexity,
"using an iterator or string search followed by `is_some()`, which is more succinctly expressed as a call to `any()` or `contains()`"
"using an iterator or string search followed by `is_some()` or `is_none()`, which is more succinctly expressed as a call to `any()` or `contains()` (with negation in case of `is_none()`)"
}

declare_clippy_lint! {
Expand Down Expand Up @@ -1636,7 +1641,7 @@ impl_lint_pass!(Methods => [
NEW_RET_NO_SELF,
SINGLE_CHAR_PATTERN,
SINGLE_CHAR_ADD_STR,
SEARCH_IS_SOME,
SEARCH_IS_SOME_OR_NONE,
FILTER_NEXT,
SKIP_WHILE_NEXT,
FILTER_MAP,
Expand Down Expand Up @@ -1721,7 +1726,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
["flat_map", ..] => flat_map_identity::check(cx, expr, arg_lists[0], method_spans[0]),
["flatten", "map"] => map_flatten::check(cx, expr, arg_lists[1]),
[option_check_method, "find"] if "is_some" == *option_check_method || "is_none" == *option_check_method => {
search_is_some::check(
search_is_some_or_none::check(
cx,
expr,
"find",
Expand All @@ -1734,7 +1739,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
[option_check_method, "position"]
if "is_some" == *option_check_method || "is_none" == *option_check_method =>
{
search_is_some::check(
search_is_some_or_none::check(
cx,
expr,
"position",
Expand All @@ -1747,7 +1752,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
[option_check_method, "rposition"]
if "is_some" == *option_check_method || "is_none" == *option_check_method =>
{
search_is_some::check(
search_is_some_or_none::check(
cx,
expr,
"rposition",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ use rustc_middle::ty;
use rustc_span::source_map::Span;
use rustc_span::symbol::sym;

use super::SEARCH_IS_SOME;
use super::SEARCH_IS_SOME_OR_NONE;

/// lint searching an Iterator followed by `is_some()`
/// or calling `find()` on a string followed by `is_some()` or `is_none()`
#[allow(clippy::too_many_lines)]
pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx hir::Expr<'_>,
Expand Down Expand Up @@ -57,7 +58,7 @@ pub(super) fn check<'tcx>(
"is_some" => {
span_lint_and_sugg(
cx,
SEARCH_IS_SOME,
SEARCH_IS_SOME_OR_NONE,
method_span.with_hi(expr.span.hi()),
&msg,
"use `any()` instead",
Expand All @@ -72,7 +73,7 @@ pub(super) fn check<'tcx>(
let iter = snippet(cx, search_args[0].span, "..");
span_lint_and_sugg(
cx,
SEARCH_IS_SOME,
SEARCH_IS_SOME_OR_NONE,
expr.span,
&msg,
"use `!_.any()` instead",
Expand All @@ -95,7 +96,7 @@ pub(super) fn check<'tcx>(
""
}
);
span_lint_and_help(cx, SEARCH_IS_SOME, expr.span, &msg, None, &hint);
span_lint_and_help(cx, SEARCH_IS_SOME_OR_NONE, expr.span, &msg, None, &hint);
}
}
// lint if `find()` is called by `String` or `&str`
Expand All @@ -119,7 +120,7 @@ pub(super) fn check<'tcx>(
let find_arg = snippet_with_applicability(cx, search_args[1].span, "..", &mut applicability);
span_lint_and_sugg(
cx,
SEARCH_IS_SOME,
SEARCH_IS_SOME_OR_NONE,
method_span.with_hi(expr.span.hi()),
&msg,
"use `contains()` instead",
Expand All @@ -133,7 +134,7 @@ pub(super) fn check<'tcx>(
let find_arg = snippet_with_applicability(cx, search_args[1].span, "..", &mut applicability);
span_lint_and_sugg(
cx,
SEARCH_IS_SOME,
SEARCH_IS_SOME_OR_NONE,
expr.span,
&msg,
"use `!_.contains()` instead",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// aux-build:option_helpers.rs
#![warn(clippy::search_is_some)]

#![warn(clippy::search_is_some_or_none)]
#![allow(dead_code)]
extern crate option_helpers;
use option_helpers::IteratorFalsePositives;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: called `is_some()` after searching an `Iterator` with `find`
--> $DIR/search_is_some.rs:14:13
--> $DIR/search_is_some_or_none.rs:14:13
|
LL | let _ = v.iter().find(|&x| {
| _____________^
Expand All @@ -8,11 +8,11 @@ LL | | }
LL | | ).is_some();
| |______________________________^
|
= note: `-D clippy::search-is-some` implied by `-D warnings`
= note: `-D clippy::search-is-some-or-none` implied by `-D warnings`
= help: this is more succinctly expressed by calling `any()`

error: called `is_some()` after searching an `Iterator` with `position`
--> $DIR/search_is_some.rs:20:13
--> $DIR/search_is_some_or_none.rs:20:13
|
LL | let _ = v.iter().position(|&x| {
| _____________^
Expand All @@ -24,7 +24,7 @@ LL | | ).is_some();
= help: this is more succinctly expressed by calling `any()`

error: called `is_some()` after searching an `Iterator` with `rposition`
--> $DIR/search_is_some.rs:26:13
--> $DIR/search_is_some_or_none.rs:26:13
|
LL | let _ = v.iter().rposition(|&x| {
| _____________^
Expand All @@ -36,7 +36,7 @@ LL | | ).is_some();
= help: this is more succinctly expressed by calling `any()`

error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some.rs:48:13
--> $DIR/search_is_some_or_none.rs:48:13
|
LL | let _ = v.iter().find(|&x| {
| _____________^
Expand All @@ -48,7 +48,7 @@ LL | | ).is_none();
= help: this is more succinctly expressed by calling `any()` with negation

error: called `is_none()` after searching an `Iterator` with `position`
--> $DIR/search_is_some.rs:54:13
--> $DIR/search_is_some_or_none.rs:54:13
|
LL | let _ = v.iter().position(|&x| {
| _____________^
Expand All @@ -60,7 +60,7 @@ LL | | ).is_none();
= help: this is more succinctly expressed by calling `any()` with negation

error: called `is_none()` after searching an `Iterator` with `rposition`
--> $DIR/search_is_some.rs:60:13
--> $DIR/search_is_some_or_none.rs:60:13
|
LL | let _ = v.iter().rposition(|&x| {
| _____________^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// run-rustfix

#![warn(clippy::search_is_some)]

#![warn(clippy::search_is_some_or_none)]
#![allow(dead_code)]
fn main() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
let y = &&42;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// run-rustfix

#![warn(clippy::search_is_some)]

#![warn(clippy::search_is_some_or_none)]
#![allow(dead_code)]
fn main() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
let y = &&42;
Expand Down
Loading

0 comments on commit a65b0d5

Please sign in to comment.