mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-27 15:12:56 +00:00
Auto merge of #7463 - ThibsG:find_any_7392, r=xFrednet
Fix `any()` not taking reference in `search_is_some` lint `find` gives reference to the item, but `any` does not, so suggestion is broken in some specific cases. Fixes: #7392 changelog: [`search_is_some`] Fix suggestion for `any()` not taking item by reference
This commit is contained in:
commit
d5d830a50f
@ -1,5 +1,6 @@
|
||||
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
|
||||
use clippy_utils::source::{snippet, snippet_with_applicability};
|
||||
use clippy_utils::sugg::deref_closure_args;
|
||||
use clippy_utils::ty::is_type_diagnostic_item;
|
||||
use clippy_utils::{is_trait_method, strip_pat_refs};
|
||||
use if_chain::if_chain;
|
||||
@ -37,6 +38,7 @@ pub(super) fn check<'tcx>(
|
||||
if search_snippet.lines().count() <= 1 {
|
||||
// suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
|
||||
// suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
|
||||
let mut applicability = Applicability::MachineApplicable;
|
||||
let any_search_snippet = if_chain! {
|
||||
if search_method == "find";
|
||||
if let hir::ExprKind::Closure(_, _, body_id, ..) = search_arg.kind;
|
||||
@ -45,9 +47,15 @@ pub(super) fn check<'tcx>(
|
||||
then {
|
||||
if let hir::PatKind::Ref(..) = closure_arg.pat.kind {
|
||||
Some(search_snippet.replacen('&', "", 1))
|
||||
} else if let PatKind::Binding(_, _, ident, _) = strip_pat_refs(closure_arg.pat).kind {
|
||||
let name = &*ident.name.as_str();
|
||||
Some(search_snippet.replace(&format!("*{}", name), name))
|
||||
} else if let PatKind::Binding(..) = strip_pat_refs(closure_arg.pat).kind {
|
||||
// `find()` provides a reference to the item, but `any` does not,
|
||||
// so we should fix item usages for suggestion
|
||||
if let Some(closure_sugg) = deref_closure_args(cx, search_arg) {
|
||||
applicability = closure_sugg.applicability;
|
||||
Some(closure_sugg.suggestion)
|
||||
} else {
|
||||
Some(search_snippet.to_string())
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -67,7 +75,7 @@ pub(super) fn check<'tcx>(
|
||||
"any({})",
|
||||
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
|
||||
),
|
||||
Applicability::MachineApplicable,
|
||||
applicability,
|
||||
);
|
||||
} else {
|
||||
let iter = snippet(cx, search_recv.span, "..");
|
||||
@ -82,7 +90,7 @@ pub(super) fn check<'tcx>(
|
||||
iter,
|
||||
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
|
||||
),
|
||||
Applicability::MachineApplicable,
|
||||
applicability,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
|
@ -1,19 +1,27 @@
|
||||
//! Contains utility functions to generate suggestions.
|
||||
#![deny(clippy::missing_docs_in_private_items)]
|
||||
|
||||
use crate::higher;
|
||||
use crate::source::{snippet, snippet_opt, snippet_with_context, snippet_with_macro_callsite};
|
||||
use crate::source::{
|
||||
snippet, snippet_opt, snippet_with_applicability, snippet_with_context, snippet_with_macro_callsite,
|
||||
};
|
||||
use crate::{get_parent_expr_for_hir, higher};
|
||||
use rustc_ast::util::parser::AssocOp;
|
||||
use rustc_ast::{ast, token};
|
||||
use rustc_ast_pretty::pprust::token_kind_to_string;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::{ExprKind, HirId, MutTy, TyKind};
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_lint::{EarlyContext, LateContext, LintContext};
|
||||
use rustc_span::source_map::{CharPos, Span};
|
||||
use rustc_span::{BytePos, Pos, SyntaxContext};
|
||||
use rustc_middle::hir::place::ProjectionKind;
|
||||
use rustc_middle::mir::{FakeReadCause, Mutability};
|
||||
use rustc_middle::ty;
|
||||
use rustc_span::source_map::{BytePos, CharPos, Pos, Span, SyntaxContext};
|
||||
use rustc_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
|
||||
use std::borrow::Cow;
|
||||
use std::convert::TryInto;
|
||||
use std::fmt::Display;
|
||||
use std::iter;
|
||||
use std::ops::{Add, Neg, Not, Sub};
|
||||
|
||||
/// A helper type to build suggestion correctly handling parentheses.
|
||||
@ -716,6 +724,267 @@ impl<T: LintContext> DiagnosticBuilderExt<T> for rustc_errors::DiagnosticBuilder
|
||||
}
|
||||
}
|
||||
|
||||
/// Suggestion results for handling closure
|
||||
/// args dereferencing and borrowing
|
||||
pub struct DerefClosure {
|
||||
/// confidence on the built suggestion
|
||||
pub applicability: Applicability,
|
||||
/// gradually built suggestion
|
||||
pub suggestion: String,
|
||||
}
|
||||
|
||||
/// Build suggestion gradually by handling closure arg specific usages,
|
||||
/// such as explicit deref and borrowing cases.
|
||||
/// Returns `None` if no such use cases have been triggered in closure body
|
||||
///
|
||||
/// note: this only works on single line immutable closures with exactly one input parameter.
|
||||
pub fn deref_closure_args<'tcx>(cx: &LateContext<'_>, closure: &'tcx hir::Expr<'_>) -> Option<DerefClosure> {
|
||||
if let hir::ExprKind::Closure(_, fn_decl, body_id, ..) = closure.kind {
|
||||
let closure_body = cx.tcx.hir().body(body_id);
|
||||
// is closure arg a type annotated double reference (i.e.: `|x: &&i32| ...`)
|
||||
// a type annotation is present if param `kind` is different from `TyKind::Infer`
|
||||
let closure_arg_is_type_annotated_double_ref = if let TyKind::Rptr(_, MutTy { ty, .. }) = fn_decl.inputs[0].kind
|
||||
{
|
||||
matches!(ty.kind, TyKind::Rptr(_, MutTy { .. }))
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let mut visitor = DerefDelegate {
|
||||
cx,
|
||||
closure_span: closure.span,
|
||||
closure_arg_is_type_annotated_double_ref,
|
||||
next_pos: closure.span.lo(),
|
||||
suggestion_start: String::new(),
|
||||
applicability: Applicability::MaybeIncorrect,
|
||||
};
|
||||
|
||||
let fn_def_id = cx.tcx.hir().local_def_id(closure.hir_id);
|
||||
cx.tcx.infer_ctxt().enter(|infcx| {
|
||||
ExprUseVisitor::new(&mut visitor, &infcx, fn_def_id, cx.param_env, cx.typeck_results())
|
||||
.consume_body(closure_body);
|
||||
});
|
||||
|
||||
if !visitor.suggestion_start.is_empty() {
|
||||
return Some(DerefClosure {
|
||||
applicability: visitor.applicability,
|
||||
suggestion: visitor.finish(),
|
||||
});
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Visitor struct used for tracking down
|
||||
/// dereferencing and borrowing of closure's args
|
||||
struct DerefDelegate<'a, 'tcx> {
|
||||
/// The late context of the lint
|
||||
cx: &'a LateContext<'tcx>,
|
||||
/// The span of the input closure to adapt
|
||||
closure_span: Span,
|
||||
/// Indicates if the arg of the closure is a type annotated double reference
|
||||
closure_arg_is_type_annotated_double_ref: bool,
|
||||
/// last position of the span to gradually build the suggestion
|
||||
next_pos: BytePos,
|
||||
/// starting part of the gradually built suggestion
|
||||
suggestion_start: String,
|
||||
/// confidence on the built suggestion
|
||||
applicability: Applicability,
|
||||
}
|
||||
|
||||
impl DerefDelegate<'_, 'tcx> {
|
||||
/// build final suggestion:
|
||||
/// - create the ending part of suggestion
|
||||
/// - concatenate starting and ending parts
|
||||
/// - potentially remove needless borrowing
|
||||
pub fn finish(&mut self) -> String {
|
||||
let end_span = Span::new(self.next_pos, self.closure_span.hi(), self.closure_span.ctxt(), None);
|
||||
let end_snip = snippet_with_applicability(self.cx, end_span, "..", &mut self.applicability);
|
||||
let sugg = format!("{}{}", self.suggestion_start, end_snip);
|
||||
if self.closure_arg_is_type_annotated_double_ref {
|
||||
sugg.replacen('&', "", 1)
|
||||
} else {
|
||||
sugg
|
||||
}
|
||||
}
|
||||
|
||||
/// indicates whether the function from `parent_expr` takes its args by double reference
|
||||
fn func_takes_arg_by_double_ref(&self, parent_expr: &'tcx hir::Expr<'_>, cmt_hir_id: HirId) -> bool {
|
||||
let (call_args, inputs) = match parent_expr.kind {
|
||||
ExprKind::MethodCall(_, _, call_args, _) => {
|
||||
if let Some(method_did) = self.cx.typeck_results().type_dependent_def_id(parent_expr.hir_id) {
|
||||
(call_args, self.cx.tcx.fn_sig(method_did).skip_binder().inputs())
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
ExprKind::Call(func, call_args) => {
|
||||
let typ = self.cx.typeck_results().expr_ty(func);
|
||||
(call_args, typ.fn_sig(self.cx.tcx).skip_binder().inputs())
|
||||
},
|
||||
_ => return false,
|
||||
};
|
||||
|
||||
iter::zip(call_args, inputs)
|
||||
.any(|(arg, ty)| arg.hir_id == cmt_hir_id && matches!(ty.kind(), ty::Ref(_, inner, _) if inner.is_ref()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
|
||||
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {
|
||||
if let PlaceBase::Local(id) = cmt.place.base {
|
||||
let map = self.cx.tcx.hir();
|
||||
let span = map.span(cmt.hir_id);
|
||||
let start_span = Span::new(self.next_pos, span.lo(), span.ctxt(), None);
|
||||
let mut start_snip = snippet_with_applicability(self.cx, start_span, "..", &mut self.applicability);
|
||||
|
||||
// identifier referring to the variable currently triggered (i.e.: `fp`)
|
||||
let ident_str = map.name(id).to_string();
|
||||
// full identifier that includes projection (i.e.: `fp.field`)
|
||||
let ident_str_with_proj = snippet(self.cx, span, "..").to_string();
|
||||
|
||||
if cmt.place.projections.is_empty() {
|
||||
// handle item without any projection, that needs an explicit borrowing
|
||||
// i.e.: suggest `&x` instead of `x`
|
||||
self.suggestion_start.push_str(&format!("{}&{}", start_snip, ident_str));
|
||||
} else {
|
||||
// cases where a parent `Call` or `MethodCall` is using the item
|
||||
// i.e.: suggest `.contains(&x)` for `.find(|x| [1, 2, 3].contains(x)).is_none()`
|
||||
//
|
||||
// Note about method calls:
|
||||
// - compiler automatically dereference references if the target type is a reference (works also for
|
||||
// function call)
|
||||
// - `self` arguments in the case of `x.is_something()` are also automatically (de)referenced, and
|
||||
// no projection should be suggested
|
||||
if let Some(parent_expr) = get_parent_expr_for_hir(self.cx, cmt.hir_id) {
|
||||
match &parent_expr.kind {
|
||||
// given expression is the self argument and will be handled completely by the compiler
|
||||
// i.e.: `|x| x.is_something()`
|
||||
ExprKind::MethodCall(_, _, [self_expr, ..], _) if self_expr.hir_id == cmt.hir_id => {
|
||||
self.suggestion_start
|
||||
.push_str(&format!("{}{}", start_snip, ident_str_with_proj));
|
||||
self.next_pos = span.hi();
|
||||
return;
|
||||
},
|
||||
// item is used in a call
|
||||
// i.e.: `Call`: `|x| please(x)` or `MethodCall`: `|x| [1, 2, 3].contains(x)`
|
||||
ExprKind::Call(_, [call_args @ ..]) | ExprKind::MethodCall(_, _, [_, call_args @ ..], _) => {
|
||||
let expr = self.cx.tcx.hir().expect_expr(cmt.hir_id);
|
||||
let arg_ty_kind = self.cx.typeck_results().expr_ty(expr).kind();
|
||||
|
||||
if matches!(arg_ty_kind, ty::Ref(_, _, Mutability::Not)) {
|
||||
// suggest ampersand if call function is taking args by double reference
|
||||
let takes_arg_by_double_ref =
|
||||
self.func_takes_arg_by_double_ref(parent_expr, cmt.hir_id);
|
||||
|
||||
// compiler will automatically dereference field or index projection, so no need
|
||||
// to suggest ampersand, but full identifier that includes projection is required
|
||||
let has_field_or_index_projection =
|
||||
cmt.place.projections.iter().any(|proj| {
|
||||
matches!(proj.kind, ProjectionKind::Field(..) | ProjectionKind::Index)
|
||||
});
|
||||
|
||||
// no need to bind again if the function doesn't take arg by double ref
|
||||
// and if the item is already a double ref
|
||||
let ident_sugg = if !call_args.is_empty()
|
||||
&& !takes_arg_by_double_ref
|
||||
&& (self.closure_arg_is_type_annotated_double_ref || has_field_or_index_projection)
|
||||
{
|
||||
let ident = if has_field_or_index_projection {
|
||||
ident_str_with_proj
|
||||
} else {
|
||||
ident_str
|
||||
};
|
||||
format!("{}{}", start_snip, ident)
|
||||
} else {
|
||||
format!("{}&{}", start_snip, ident_str)
|
||||
};
|
||||
self.suggestion_start.push_str(&ident_sugg);
|
||||
self.next_pos = span.hi();
|
||||
return;
|
||||
}
|
||||
|
||||
self.applicability = Applicability::Unspecified;
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
let mut replacement_str = ident_str;
|
||||
let mut projections_handled = false;
|
||||
cmt.place.projections.iter().enumerate().for_each(|(i, proj)| {
|
||||
match proj.kind {
|
||||
// Field projection like `|v| v.foo`
|
||||
// no adjustment needed here, as field projections are handled by the compiler
|
||||
ProjectionKind::Field(..) => match cmt.place.ty_before_projection(i).kind() {
|
||||
ty::Adt(..) | ty::Tuple(_) => {
|
||||
replacement_str = ident_str_with_proj.clone();
|
||||
projections_handled = true;
|
||||
},
|
||||
_ => (),
|
||||
},
|
||||
// Index projection like `|x| foo[x]`
|
||||
// the index is dropped so we can't get it to build the suggestion,
|
||||
// so the span is set-up again to get more code, using `span.hi()` (i.e.: `foo[x]`)
|
||||
// instead of `span.lo()` (i.e.: `foo`)
|
||||
ProjectionKind::Index => {
|
||||
let start_span = Span::new(self.next_pos, span.hi(), span.ctxt(), None);
|
||||
start_snip = snippet_with_applicability(self.cx, start_span, "..", &mut self.applicability);
|
||||
replacement_str.clear();
|
||||
projections_handled = true;
|
||||
},
|
||||
// note: unable to trigger `Subslice` kind in tests
|
||||
ProjectionKind::Subslice => (),
|
||||
ProjectionKind::Deref => {
|
||||
// Explicit derefs are typically handled later on, but
|
||||
// some items do not need explicit deref, such as array accesses,
|
||||
// so we mark them as already processed
|
||||
// i.e.: don't suggest `*sub[1..4].len()` for `|sub| sub[1..4].len() == 3`
|
||||
if let ty::Ref(_, inner, _) = cmt.place.ty_before_projection(i).kind() {
|
||||
if matches!(inner.kind(), ty::Ref(_, innermost, _) if innermost.is_array()) {
|
||||
projections_handled = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
// handle `ProjectionKind::Deref` by removing one explicit deref
|
||||
// if no special case was detected (i.e.: suggest `*x` instead of `**x`)
|
||||
if !projections_handled {
|
||||
let last_deref = cmt
|
||||
.place
|
||||
.projections
|
||||
.iter()
|
||||
.rposition(|proj| proj.kind == ProjectionKind::Deref);
|
||||
|
||||
if let Some(pos) = last_deref {
|
||||
let mut projections = cmt.place.projections.clone();
|
||||
projections.truncate(pos);
|
||||
|
||||
for item in projections {
|
||||
if item.kind == ProjectionKind::Deref {
|
||||
replacement_str = format!("*{}", replacement_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.suggestion_start
|
||||
.push_str(&format!("{}{}", start_snip, replacement_str));
|
||||
}
|
||||
self.next_pos = span.hi();
|
||||
}
|
||||
}
|
||||
|
||||
fn mutate(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
|
||||
|
||||
fn fake_read(&mut self, _: rustc_typeck::expr_use_visitor::Place<'tcx>, _: FakeReadCause, _: HirId) {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::Sugg;
|
||||
|
@ -36,6 +36,9 @@ fn main() {
|
||||
// check that we don't lint if `find()` is called with
|
||||
// `Pattern` that is not a string
|
||||
let _ = "hello world".find(|c: char| c == 'o' || c == 'l').is_some();
|
||||
|
||||
let some_closure = |x: &u32| *x == 0;
|
||||
let _ = (0..1).find(some_closure).is_some();
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
@ -70,4 +73,7 @@ fn is_none() {
|
||||
// check that we don't lint if `find()` is called with
|
||||
// `Pattern` that is not a string
|
||||
let _ = "hello world".find(|c: char| c == 'o' || c == 'l').is_none();
|
||||
|
||||
let some_closure = |x: &u32| *x == 0;
|
||||
let _ = (0..1).find(some_closure).is_none();
|
||||
}
|
||||
|
@ -35,8 +35,14 @@ LL | | ).is_some();
|
||||
|
|
||||
= help: this is more succinctly expressed by calling `any()`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some.rs:41:20
|
||||
|
|
||||
LL | let _ = (0..1).find(some_closure).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(some_closure)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some.rs:48:13
|
||||
--> $DIR/search_is_some.rs:51:13
|
||||
|
|
||||
LL | let _ = v.iter().find(|&x| {
|
||||
| _____________^
|
||||
@ -48,7 +54,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.rs:57:13
|
||||
|
|
||||
LL | let _ = v.iter().position(|&x| {
|
||||
| _____________^
|
||||
@ -60,7 +66,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.rs:63:13
|
||||
|
|
||||
LL | let _ = v.iter().rposition(|&x| {
|
||||
| _____________^
|
||||
@ -71,5 +77,11 @@ LL | | ).is_none();
|
||||
|
|
||||
= help: this is more succinctly expressed by calling `any()` with negation
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some.rs:78:13
|
||||
|
|
||||
LL | let _ = (0..1).find(some_closure).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(0..1).any(some_closure)`
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
|
||||
|
@ -1,68 +0,0 @@
|
||||
// run-rustfix
|
||||
#![allow(dead_code)]
|
||||
#![warn(clippy::search_is_some)]
|
||||
|
||||
fn main() {
|
||||
let v = vec![3, 2, 1, 0, -1, -2, -3];
|
||||
let y = &&42;
|
||||
|
||||
// Check `find().is_some()`, single-line case.
|
||||
let _ = v.iter().any(|x| *x < 0);
|
||||
let _ = (0..1).any(|x| **y == x); // one dereference less
|
||||
let _ = (0..1).any(|x| x == 0);
|
||||
let _ = v.iter().any(|x| *x == 0);
|
||||
|
||||
// Check `position().is_some()`, single-line case.
|
||||
let _ = v.iter().any(|&x| x < 0);
|
||||
|
||||
// Check `rposition().is_some()`, single-line case.
|
||||
let _ = v.iter().any(|&x| x < 0);
|
||||
|
||||
let s1 = String::from("hello world");
|
||||
let s2 = String::from("world");
|
||||
// caller of `find()` is a `&`static str`
|
||||
let _ = "hello world".contains("world");
|
||||
let _ = "hello world".contains(&s2);
|
||||
let _ = "hello world".contains(&s2[2..]);
|
||||
// caller of `find()` is a `String`
|
||||
let _ = s1.contains("world");
|
||||
let _ = s1.contains(&s2);
|
||||
let _ = s1.contains(&s2[2..]);
|
||||
// caller of `find()` is slice of `String`
|
||||
let _ = s1[2..].contains("world");
|
||||
let _ = s1[2..].contains(&s2);
|
||||
let _ = s1[2..].contains(&s2[2..]);
|
||||
}
|
||||
|
||||
fn is_none() {
|
||||
let v = vec![3, 2, 1, 0, -1, -2, -3];
|
||||
let y = &&42;
|
||||
|
||||
// Check `find().is_none()`, single-line case.
|
||||
let _ = !v.iter().any(|x| *x < 0);
|
||||
let _ = !(0..1).any(|x| **y == x); // one dereference less
|
||||
let _ = !(0..1).any(|x| x == 0);
|
||||
let _ = !v.iter().any(|x| *x == 0);
|
||||
|
||||
// Check `position().is_none()`, single-line case.
|
||||
let _ = !v.iter().any(|&x| x < 0);
|
||||
|
||||
// Check `rposition().is_none()`, single-line case.
|
||||
let _ = !v.iter().any(|&x| x < 0);
|
||||
|
||||
let s1 = String::from("hello world");
|
||||
let s2 = String::from("world");
|
||||
|
||||
// caller of `find()` is a `&`static str`
|
||||
let _ = !"hello world".contains("world");
|
||||
let _ = !"hello world".contains(&s2);
|
||||
let _ = !"hello world".contains(&s2[2..]);
|
||||
// caller of `find()` is a `String`
|
||||
let _ = !s1.contains("world");
|
||||
let _ = !s1.contains(&s2);
|
||||
let _ = !s1.contains(&s2[2..]);
|
||||
// caller of `find()` is slice of `String`
|
||||
let _ = !s1[2..].contains("world");
|
||||
let _ = !s1[2..].contains(&s2);
|
||||
let _ = !s1[2..].contains(&s2[2..]);
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
// run-rustfix
|
||||
#![allow(dead_code)]
|
||||
#![warn(clippy::search_is_some)]
|
||||
|
||||
fn main() {
|
||||
let v = vec![3, 2, 1, 0, -1, -2, -3];
|
||||
let y = &&42;
|
||||
|
||||
// Check `find().is_some()`, single-line case.
|
||||
let _ = v.iter().find(|&x| *x < 0).is_some();
|
||||
let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
|
||||
let _ = (0..1).find(|x| *x == 0).is_some();
|
||||
let _ = v.iter().find(|x| **x == 0).is_some();
|
||||
|
||||
// Check `position().is_some()`, single-line case.
|
||||
let _ = v.iter().position(|&x| x < 0).is_some();
|
||||
|
||||
// Check `rposition().is_some()`, single-line case.
|
||||
let _ = v.iter().rposition(|&x| x < 0).is_some();
|
||||
|
||||
let s1 = String::from("hello world");
|
||||
let s2 = String::from("world");
|
||||
// caller of `find()` is a `&`static str`
|
||||
let _ = "hello world".find("world").is_some();
|
||||
let _ = "hello world".find(&s2).is_some();
|
||||
let _ = "hello world".find(&s2[2..]).is_some();
|
||||
// caller of `find()` is a `String`
|
||||
let _ = s1.find("world").is_some();
|
||||
let _ = s1.find(&s2).is_some();
|
||||
let _ = s1.find(&s2[2..]).is_some();
|
||||
// caller of `find()` is slice of `String`
|
||||
let _ = s1[2..].find("world").is_some();
|
||||
let _ = s1[2..].find(&s2).is_some();
|
||||
let _ = s1[2..].find(&s2[2..]).is_some();
|
||||
}
|
||||
|
||||
fn is_none() {
|
||||
let v = vec![3, 2, 1, 0, -1, -2, -3];
|
||||
let y = &&42;
|
||||
|
||||
// Check `find().is_none()`, single-line case.
|
||||
let _ = v.iter().find(|&x| *x < 0).is_none();
|
||||
let _ = (0..1).find(|x| **y == *x).is_none(); // one dereference less
|
||||
let _ = (0..1).find(|x| *x == 0).is_none();
|
||||
let _ = v.iter().find(|x| **x == 0).is_none();
|
||||
|
||||
// Check `position().is_none()`, single-line case.
|
||||
let _ = v.iter().position(|&x| x < 0).is_none();
|
||||
|
||||
// Check `rposition().is_none()`, single-line case.
|
||||
let _ = v.iter().rposition(|&x| x < 0).is_none();
|
||||
|
||||
let s1 = String::from("hello world");
|
||||
let s2 = String::from("world");
|
||||
|
||||
// caller of `find()` is a `&`static str`
|
||||
let _ = "hello world".find("world").is_none();
|
||||
let _ = "hello world".find(&s2).is_none();
|
||||
let _ = "hello world".find(&s2[2..]).is_none();
|
||||
// caller of `find()` is a `String`
|
||||
let _ = s1.find("world").is_none();
|
||||
let _ = s1.find(&s2).is_none();
|
||||
let _ = s1.find(&s2[2..]).is_none();
|
||||
// caller of `find()` is slice of `String`
|
||||
let _ = s1[2..].find("world").is_none();
|
||||
let _ = s1[2..].find(&s2).is_none();
|
||||
let _ = s1[2..].find(&s2[2..]).is_none();
|
||||
}
|
@ -1,184 +0,0 @@
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable.rs:10:22
|
||||
|
|
||||
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| *x < 0)`
|
||||
|
|
||||
= note: `-D clippy::search-is-some` implied by `-D warnings`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable.rs:11:20
|
||||
|
|
||||
LL | let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| **y == x)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable.rs:12:20
|
||||
|
|
||||
LL | let _ = (0..1).find(|x| *x == 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| x == 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable.rs:13:22
|
||||
|
|
||||
LL | let _ = v.iter().find(|x| **x == 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| *x == 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `position`
|
||||
--> $DIR/search_is_some_fixable.rs:16:22
|
||||
|
|
||||
LL | let _ = v.iter().position(|&x| x < 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|&x| x < 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `rposition`
|
||||
--> $DIR/search_is_some_fixable.rs:19:22
|
||||
|
|
||||
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|&x| x < 0)`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:24:27
|
||||
|
|
||||
LL | let _ = "hello world".find("world").is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains("world")`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:25:27
|
||||
|
|
||||
LL | let _ = "hello world".find(&s2).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2)`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:26:27
|
||||
|
|
||||
LL | let _ = "hello world".find(&s2[2..]).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:28:16
|
||||
|
|
||||
LL | let _ = s1.find("world").is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains("world")`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:29:16
|
||||
|
|
||||
LL | let _ = s1.find(&s2).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2)`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:30:16
|
||||
|
|
||||
LL | let _ = s1.find(&s2[2..]).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:32:21
|
||||
|
|
||||
LL | let _ = s1[2..].find("world").is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains("world")`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:33:21
|
||||
|
|
||||
LL | let _ = s1[2..].find(&s2).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2)`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:34:21
|
||||
|
|
||||
LL | let _ = s1[2..].find(&s2[2..]).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable.rs:42:13
|
||||
|
|
||||
LL | let _ = v.iter().find(|&x| *x < 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| *x < 0)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable.rs:43:13
|
||||
|
|
||||
LL | let _ = (0..1).find(|x| **y == *x).is_none(); // one dereference less
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(0..1).any(|x| **y == x)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable.rs:44:13
|
||||
|
|
||||
LL | let _ = (0..1).find(|x| *x == 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(0..1).any(|x| x == 0)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable.rs:45:13
|
||||
|
|
||||
LL | let _ = v.iter().find(|x| **x == 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| *x == 0)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `position`
|
||||
--> $DIR/search_is_some_fixable.rs:48:13
|
||||
|
|
||||
LL | let _ = v.iter().position(|&x| x < 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|&x| x < 0)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `rposition`
|
||||
--> $DIR/search_is_some_fixable.rs:51:13
|
||||
|
|
||||
LL | let _ = v.iter().rposition(|&x| x < 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|&x| x < 0)`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:57:13
|
||||
|
|
||||
LL | let _ = "hello world".find("world").is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!"hello world".contains("world")`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:58:13
|
||||
|
|
||||
LL | let _ = "hello world".find(&s2).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!"hello world".contains(&s2)`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:59:13
|
||||
|
|
||||
LL | let _ = "hello world".find(&s2[2..]).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!"hello world".contains(&s2[2..])`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:61:13
|
||||
|
|
||||
LL | let _ = s1.find("world").is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1.contains("world")`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:62:13
|
||||
|
|
||||
LL | let _ = s1.find(&s2).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1.contains(&s2)`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:63:13
|
||||
|
|
||||
LL | let _ = s1.find(&s2[2..]).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1.contains(&s2[2..])`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:65:13
|
||||
|
|
||||
LL | let _ = s1[2..].find("world").is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1[2..].contains("world")`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:66:13
|
||||
|
|
||||
LL | let _ = s1[2..].find(&s2).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1[2..].contains(&s2)`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable.rs:67:13
|
||||
|
|
||||
LL | let _ = s1[2..].find(&s2[2..]).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1[2..].contains(&s2[2..])`
|
||||
|
||||
error: aborting due to 30 previous errors
|
||||
|
216
tests/ui/search_is_some_fixable_none.fixed
Normal file
216
tests/ui/search_is_some_fixable_none.fixed
Normal file
@ -0,0 +1,216 @@
|
||||
// run-rustfix
|
||||
#![allow(dead_code)]
|
||||
#![warn(clippy::search_is_some)]
|
||||
|
||||
fn main() {
|
||||
let v = vec![3, 2, 1, 0, -1, -2, -3];
|
||||
let y = &&42;
|
||||
|
||||
// Check `find().is_none()`, single-line case.
|
||||
let _ = !v.iter().any(|x| *x < 0);
|
||||
let _ = !(0..1).any(|x| **y == x); // one dereference less
|
||||
let _ = !(0..1).any(|x| x == 0);
|
||||
let _ = !v.iter().any(|x| *x == 0);
|
||||
let _ = !(4..5).any(|x| x == 1 || x == 3 || x == 5);
|
||||
let _ = !(1..3).any(|x| [1, 2, 3].contains(&x));
|
||||
let _ = !(1..3).any(|x| x == 0 || [1, 2, 3].contains(&x));
|
||||
let _ = !(1..3).any(|x| [1, 2, 3].contains(&x) || x == 0);
|
||||
let _ = !(1..3).any(|x| [1, 2, 3].contains(&x) || x == 0 || [4, 5, 6].contains(&x) || x == -1);
|
||||
|
||||
// Check `position().is_none()`, single-line case.
|
||||
let _ = !v.iter().any(|&x| x < 0);
|
||||
|
||||
// Check `rposition().is_none()`, single-line case.
|
||||
let _ = !v.iter().any(|&x| x < 0);
|
||||
|
||||
let s1 = String::from("hello world");
|
||||
let s2 = String::from("world");
|
||||
|
||||
// caller of `find()` is a `&`static str`
|
||||
let _ = !"hello world".contains("world");
|
||||
let _ = !"hello world".contains(&s2);
|
||||
let _ = !"hello world".contains(&s2[2..]);
|
||||
// caller of `find()` is a `String`
|
||||
let _ = !s1.contains("world");
|
||||
let _ = !s1.contains(&s2);
|
||||
let _ = !s1.contains(&s2[2..]);
|
||||
// caller of `find()` is slice of `String`
|
||||
let _ = !s1[2..].contains("world");
|
||||
let _ = !s1[2..].contains(&s2);
|
||||
let _ = !s1[2..].contains(&s2[2..]);
|
||||
}
|
||||
|
||||
#[allow(clippy::clone_on_copy, clippy::map_clone)]
|
||||
mod issue7392 {
|
||||
struct Player {
|
||||
hand: Vec<usize>,
|
||||
}
|
||||
fn filter() {
|
||||
let p = Player {
|
||||
hand: vec![1, 2, 3, 4, 5],
|
||||
};
|
||||
let filter_hand = vec![5];
|
||||
let _ = p
|
||||
.hand
|
||||
.iter()
|
||||
.filter(|c| !filter_hand.iter().any(|cc| c == &cc))
|
||||
.map(|c| c.clone())
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
struct PlayerTuple {
|
||||
hand: Vec<(usize, char)>,
|
||||
}
|
||||
fn filter_tuple() {
|
||||
let p = PlayerTuple {
|
||||
hand: vec![(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')],
|
||||
};
|
||||
let filter_hand = vec![5];
|
||||
let _ = p
|
||||
.hand
|
||||
.iter()
|
||||
.filter(|(c, _)| !filter_hand.iter().any(|cc| c == cc))
|
||||
.map(|c| c.clone())
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
fn field_projection() {
|
||||
struct Foo {
|
||||
foo: i32,
|
||||
bar: u32,
|
||||
}
|
||||
let vfoo = vec![Foo { foo: 1, bar: 2 }];
|
||||
let _ = !vfoo.iter().any(|v| v.foo == 1 && v.bar == 2);
|
||||
|
||||
let vfoo = vec![(42, Foo { foo: 1, bar: 2 })];
|
||||
let _ = !vfoo
|
||||
.iter().any(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2);
|
||||
}
|
||||
|
||||
fn index_projection() {
|
||||
let vfoo = vec![[0, 1, 2, 3]];
|
||||
let _ = !vfoo.iter().any(|a| a[0] == 42);
|
||||
}
|
||||
|
||||
#[allow(clippy::match_like_matches_macro)]
|
||||
fn slice_projection() {
|
||||
let vfoo = vec![[0, 1, 2, 3, 0, 1, 2, 3]];
|
||||
let _ = !vfoo.iter().any(|sub| sub[1..4].len() == 3);
|
||||
}
|
||||
|
||||
fn please(x: &u32) -> bool {
|
||||
*x == 9
|
||||
}
|
||||
|
||||
fn deref_enough(x: u32) -> bool {
|
||||
x == 78
|
||||
}
|
||||
|
||||
fn arg_no_deref(x: &&u32) -> bool {
|
||||
**x == 78
|
||||
}
|
||||
|
||||
fn more_projections() {
|
||||
let x = 19;
|
||||
let ppx: &u32 = &x;
|
||||
let _ = ![ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
|
||||
let _ = ![String::from("Hey hey")].iter().any(|s| s.len() == 2);
|
||||
|
||||
let v = vec![3, 2, 1, 0];
|
||||
let _ = !v.iter().any(|x| deref_enough(*x));
|
||||
let _ = !v.iter().any(|x: &u32| deref_enough(*x));
|
||||
|
||||
#[allow(clippy::redundant_closure)]
|
||||
let _ = !v.iter().any(|x| arg_no_deref(&x));
|
||||
#[allow(clippy::redundant_closure)]
|
||||
let _ = !v.iter().any(|x: &u32| arg_no_deref(&x));
|
||||
}
|
||||
|
||||
fn field_index_projection() {
|
||||
struct FooDouble {
|
||||
bar: Vec<Vec<i32>>,
|
||||
}
|
||||
struct Foo {
|
||||
bar: Vec<i32>,
|
||||
}
|
||||
struct FooOuter {
|
||||
inner: Foo,
|
||||
inner_double: FooDouble,
|
||||
}
|
||||
let vfoo = vec![FooOuter {
|
||||
inner: Foo { bar: vec![0, 1, 2, 3] },
|
||||
inner_double: FooDouble {
|
||||
bar: vec![vec![0, 1, 2, 3]],
|
||||
},
|
||||
}];
|
||||
let _ = !vfoo
|
||||
.iter().any(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2);
|
||||
}
|
||||
|
||||
fn index_field_projection() {
|
||||
struct Foo {
|
||||
bar: i32,
|
||||
}
|
||||
struct FooOuter {
|
||||
inner: Vec<Foo>,
|
||||
}
|
||||
let vfoo = vec![FooOuter {
|
||||
inner: vec![Foo { bar: 0 }],
|
||||
}];
|
||||
let _ = !vfoo.iter().any(|v| v.inner[0].bar == 2);
|
||||
}
|
||||
|
||||
fn double_deref_index_projection() {
|
||||
let vfoo = vec![&&[0, 1, 2, 3]];
|
||||
let _ = !vfoo.iter().any(|x| (**x)[0] == 9);
|
||||
}
|
||||
|
||||
fn method_call_by_ref() {
|
||||
struct Foo {
|
||||
bar: u32,
|
||||
}
|
||||
impl Foo {
|
||||
pub fn by_ref(&self, x: &u32) -> bool {
|
||||
*x == self.bar
|
||||
}
|
||||
}
|
||||
let vfoo = vec![Foo { bar: 1 }];
|
||||
let _ = !vfoo.iter().any(|v| v.by_ref(&v.bar));
|
||||
}
|
||||
|
||||
fn ref_bindings() {
|
||||
let _ = ![&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y);
|
||||
let _ = ![&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y);
|
||||
}
|
||||
|
||||
fn test_string_1(s: &str) -> bool {
|
||||
s.is_empty()
|
||||
}
|
||||
|
||||
fn test_u32_1(s: &u32) -> bool {
|
||||
s.is_power_of_two()
|
||||
}
|
||||
|
||||
fn test_u32_2(s: u32) -> bool {
|
||||
s.is_power_of_two()
|
||||
}
|
||||
|
||||
fn projection_in_args_test() {
|
||||
// Index projections
|
||||
let lst = &[String::from("Hello"), String::from("world")];
|
||||
let v: Vec<&[String]> = vec![lst];
|
||||
let _ = !v.iter().any(|s| s[0].is_empty());
|
||||
let _ = !v.iter().any(|s| test_string_1(&s[0]));
|
||||
|
||||
// Field projections
|
||||
struct FieldProjection<'a> {
|
||||
field: &'a u32,
|
||||
}
|
||||
let field = 123456789;
|
||||
let instance = FieldProjection { field: &field };
|
||||
let v = vec![instance];
|
||||
let _ = !v.iter().any(|fp| fp.field.is_power_of_two());
|
||||
let _ = !v.iter().any(|fp| test_u32_1(fp.field));
|
||||
let _ = !v.iter().any(|fp| test_u32_2(*fp.field));
|
||||
}
|
||||
}
|
222
tests/ui/search_is_some_fixable_none.rs
Normal file
222
tests/ui/search_is_some_fixable_none.rs
Normal file
@ -0,0 +1,222 @@
|
||||
// run-rustfix
|
||||
#![allow(dead_code)]
|
||||
#![warn(clippy::search_is_some)]
|
||||
|
||||
fn main() {
|
||||
let v = vec![3, 2, 1, 0, -1, -2, -3];
|
||||
let y = &&42;
|
||||
|
||||
// Check `find().is_none()`, single-line case.
|
||||
let _ = v.iter().find(|&x| *x < 0).is_none();
|
||||
let _ = (0..1).find(|x| **y == *x).is_none(); // one dereference less
|
||||
let _ = (0..1).find(|x| *x == 0).is_none();
|
||||
let _ = v.iter().find(|x| **x == 0).is_none();
|
||||
let _ = (4..5).find(|x| *x == 1 || *x == 3 || *x == 5).is_none();
|
||||
let _ = (1..3).find(|x| [1, 2, 3].contains(x)).is_none();
|
||||
let _ = (1..3).find(|x| *x == 0 || [1, 2, 3].contains(x)).is_none();
|
||||
let _ = (1..3).find(|x| [1, 2, 3].contains(x) || *x == 0).is_none();
|
||||
let _ = (1..3)
|
||||
.find(|x| [1, 2, 3].contains(x) || *x == 0 || [4, 5, 6].contains(x) || *x == -1)
|
||||
.is_none();
|
||||
|
||||
// Check `position().is_none()`, single-line case.
|
||||
let _ = v.iter().position(|&x| x < 0).is_none();
|
||||
|
||||
// Check `rposition().is_none()`, single-line case.
|
||||
let _ = v.iter().rposition(|&x| x < 0).is_none();
|
||||
|
||||
let s1 = String::from("hello world");
|
||||
let s2 = String::from("world");
|
||||
|
||||
// caller of `find()` is a `&`static str`
|
||||
let _ = "hello world".find("world").is_none();
|
||||
let _ = "hello world".find(&s2).is_none();
|
||||
let _ = "hello world".find(&s2[2..]).is_none();
|
||||
// caller of `find()` is a `String`
|
||||
let _ = s1.find("world").is_none();
|
||||
let _ = s1.find(&s2).is_none();
|
||||
let _ = s1.find(&s2[2..]).is_none();
|
||||
// caller of `find()` is slice of `String`
|
||||
let _ = s1[2..].find("world").is_none();
|
||||
let _ = s1[2..].find(&s2).is_none();
|
||||
let _ = s1[2..].find(&s2[2..]).is_none();
|
||||
}
|
||||
|
||||
#[allow(clippy::clone_on_copy, clippy::map_clone)]
|
||||
mod issue7392 {
|
||||
struct Player {
|
||||
hand: Vec<usize>,
|
||||
}
|
||||
fn filter() {
|
||||
let p = Player {
|
||||
hand: vec![1, 2, 3, 4, 5],
|
||||
};
|
||||
let filter_hand = vec![5];
|
||||
let _ = p
|
||||
.hand
|
||||
.iter()
|
||||
.filter(|c| filter_hand.iter().find(|cc| c == cc).is_none())
|
||||
.map(|c| c.clone())
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
struct PlayerTuple {
|
||||
hand: Vec<(usize, char)>,
|
||||
}
|
||||
fn filter_tuple() {
|
||||
let p = PlayerTuple {
|
||||
hand: vec![(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')],
|
||||
};
|
||||
let filter_hand = vec![5];
|
||||
let _ = p
|
||||
.hand
|
||||
.iter()
|
||||
.filter(|(c, _)| filter_hand.iter().find(|cc| c == *cc).is_none())
|
||||
.map(|c| c.clone())
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
fn field_projection() {
|
||||
struct Foo {
|
||||
foo: i32,
|
||||
bar: u32,
|
||||
}
|
||||
let vfoo = vec![Foo { foo: 1, bar: 2 }];
|
||||
let _ = vfoo.iter().find(|v| v.foo == 1 && v.bar == 2).is_none();
|
||||
|
||||
let vfoo = vec![(42, Foo { foo: 1, bar: 2 })];
|
||||
let _ = vfoo
|
||||
.iter()
|
||||
.find(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2)
|
||||
.is_none();
|
||||
}
|
||||
|
||||
fn index_projection() {
|
||||
let vfoo = vec![[0, 1, 2, 3]];
|
||||
let _ = vfoo.iter().find(|a| a[0] == 42).is_none();
|
||||
}
|
||||
|
||||
#[allow(clippy::match_like_matches_macro)]
|
||||
fn slice_projection() {
|
||||
let vfoo = vec![[0, 1, 2, 3, 0, 1, 2, 3]];
|
||||
let _ = vfoo.iter().find(|sub| sub[1..4].len() == 3).is_none();
|
||||
}
|
||||
|
||||
fn please(x: &u32) -> bool {
|
||||
*x == 9
|
||||
}
|
||||
|
||||
fn deref_enough(x: u32) -> bool {
|
||||
x == 78
|
||||
}
|
||||
|
||||
fn arg_no_deref(x: &&u32) -> bool {
|
||||
**x == 78
|
||||
}
|
||||
|
||||
fn more_projections() {
|
||||
let x = 19;
|
||||
let ppx: &u32 = &x;
|
||||
let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_none();
|
||||
let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_none();
|
||||
|
||||
let v = vec![3, 2, 1, 0];
|
||||
let _ = v.iter().find(|x| deref_enough(**x)).is_none();
|
||||
let _ = v.iter().find(|x: &&u32| deref_enough(**x)).is_none();
|
||||
|
||||
#[allow(clippy::redundant_closure)]
|
||||
let _ = v.iter().find(|x| arg_no_deref(x)).is_none();
|
||||
#[allow(clippy::redundant_closure)]
|
||||
let _ = v.iter().find(|x: &&u32| arg_no_deref(x)).is_none();
|
||||
}
|
||||
|
||||
fn field_index_projection() {
|
||||
struct FooDouble {
|
||||
bar: Vec<Vec<i32>>,
|
||||
}
|
||||
struct Foo {
|
||||
bar: Vec<i32>,
|
||||
}
|
||||
struct FooOuter {
|
||||
inner: Foo,
|
||||
inner_double: FooDouble,
|
||||
}
|
||||
let vfoo = vec![FooOuter {
|
||||
inner: Foo { bar: vec![0, 1, 2, 3] },
|
||||
inner_double: FooDouble {
|
||||
bar: vec![vec![0, 1, 2, 3]],
|
||||
},
|
||||
}];
|
||||
let _ = vfoo
|
||||
.iter()
|
||||
.find(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2)
|
||||
.is_none();
|
||||
}
|
||||
|
||||
fn index_field_projection() {
|
||||
struct Foo {
|
||||
bar: i32,
|
||||
}
|
||||
struct FooOuter {
|
||||
inner: Vec<Foo>,
|
||||
}
|
||||
let vfoo = vec![FooOuter {
|
||||
inner: vec![Foo { bar: 0 }],
|
||||
}];
|
||||
let _ = vfoo.iter().find(|v| v.inner[0].bar == 2).is_none();
|
||||
}
|
||||
|
||||
fn double_deref_index_projection() {
|
||||
let vfoo = vec![&&[0, 1, 2, 3]];
|
||||
let _ = vfoo.iter().find(|x| (**x)[0] == 9).is_none();
|
||||
}
|
||||
|
||||
fn method_call_by_ref() {
|
||||
struct Foo {
|
||||
bar: u32,
|
||||
}
|
||||
impl Foo {
|
||||
pub fn by_ref(&self, x: &u32) -> bool {
|
||||
*x == self.bar
|
||||
}
|
||||
}
|
||||
let vfoo = vec![Foo { bar: 1 }];
|
||||
let _ = vfoo.iter().find(|v| v.by_ref(&v.bar)).is_none();
|
||||
}
|
||||
|
||||
fn ref_bindings() {
|
||||
let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|(&x, y)| x == *y).is_none();
|
||||
let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|&(&x, y)| x == *y).is_none();
|
||||
}
|
||||
|
||||
fn test_string_1(s: &String) -> bool {
|
||||
s.is_empty()
|
||||
}
|
||||
|
||||
fn test_u32_1(s: &u32) -> bool {
|
||||
s.is_power_of_two()
|
||||
}
|
||||
|
||||
fn test_u32_2(s: u32) -> bool {
|
||||
s.is_power_of_two()
|
||||
}
|
||||
|
||||
fn projection_in_args_test() {
|
||||
// Index projections
|
||||
let lst = &[String::from("Hello"), String::from("world")];
|
||||
let v: Vec<&[String]> = vec![lst];
|
||||
let _ = v.iter().find(|s| s[0].is_empty()).is_none();
|
||||
let _ = v.iter().find(|s| test_string_1(&s[0])).is_none();
|
||||
|
||||
// Field projections
|
||||
struct FieldProjection<'a> {
|
||||
field: &'a u32,
|
||||
}
|
||||
let field = 123456789;
|
||||
let instance = FieldProjection { field: &field };
|
||||
let v = vec![instance];
|
||||
let _ = v.iter().find(|fp| fp.field.is_power_of_two()).is_none();
|
||||
let _ = v.iter().find(|fp| test_u32_1(fp.field)).is_none();
|
||||
let _ = v.iter().find(|fp| test_u32_2(*fp.field)).is_none();
|
||||
}
|
||||
}
|
293
tests/ui/search_is_some_fixable_none.stderr
Normal file
293
tests/ui/search_is_some_fixable_none.stderr
Normal file
@ -0,0 +1,293 @@
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:10:13
|
||||
|
|
||||
LL | let _ = v.iter().find(|&x| *x < 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| *x < 0)`
|
||||
|
|
||||
= note: `-D clippy::search-is-some` implied by `-D warnings`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:11:13
|
||||
|
|
||||
LL | let _ = (0..1).find(|x| **y == *x).is_none(); // one dereference less
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(0..1).any(|x| **y == x)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:12:13
|
||||
|
|
||||
LL | let _ = (0..1).find(|x| *x == 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(0..1).any(|x| x == 0)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:13:13
|
||||
|
|
||||
LL | let _ = v.iter().find(|x| **x == 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| *x == 0)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:14:13
|
||||
|
|
||||
LL | let _ = (4..5).find(|x| *x == 1 || *x == 3 || *x == 5).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(4..5).any(|x| x == 1 || x == 3 || x == 5)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:15:13
|
||||
|
|
||||
LL | let _ = (1..3).find(|x| [1, 2, 3].contains(x)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(1..3).any(|x| [1, 2, 3].contains(&x))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:16:13
|
||||
|
|
||||
LL | let _ = (1..3).find(|x| *x == 0 || [1, 2, 3].contains(x)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(1..3).any(|x| x == 0 || [1, 2, 3].contains(&x))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:17:13
|
||||
|
|
||||
LL | let _ = (1..3).find(|x| [1, 2, 3].contains(x) || *x == 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(1..3).any(|x| [1, 2, 3].contains(&x) || x == 0)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:18:13
|
||||
|
|
||||
LL | let _ = (1..3)
|
||||
| _____________^
|
||||
LL | | .find(|x| [1, 2, 3].contains(x) || *x == 0 || [4, 5, 6].contains(x) || *x == -1)
|
||||
LL | | .is_none();
|
||||
| |__________________^ help: use `!_.any()` instead: `!(1..3).any(|x| [1, 2, 3].contains(&x) || x == 0 || [4, 5, 6].contains(&x) || x == -1)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `position`
|
||||
--> $DIR/search_is_some_fixable_none.rs:23:13
|
||||
|
|
||||
LL | let _ = v.iter().position(|&x| x < 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|&x| x < 0)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `rposition`
|
||||
--> $DIR/search_is_some_fixable_none.rs:26:13
|
||||
|
|
||||
LL | let _ = v.iter().rposition(|&x| x < 0).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|&x| x < 0)`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_none.rs:32:13
|
||||
|
|
||||
LL | let _ = "hello world".find("world").is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!"hello world".contains("world")`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_none.rs:33:13
|
||||
|
|
||||
LL | let _ = "hello world".find(&s2).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!"hello world".contains(&s2)`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_none.rs:34:13
|
||||
|
|
||||
LL | let _ = "hello world".find(&s2[2..]).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!"hello world".contains(&s2[2..])`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_none.rs:36:13
|
||||
|
|
||||
LL | let _ = s1.find("world").is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1.contains("world")`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_none.rs:37:13
|
||||
|
|
||||
LL | let _ = s1.find(&s2).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1.contains(&s2)`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_none.rs:38:13
|
||||
|
|
||||
LL | let _ = s1.find(&s2[2..]).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1.contains(&s2[2..])`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_none.rs:40:13
|
||||
|
|
||||
LL | let _ = s1[2..].find("world").is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1[2..].contains("world")`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_none.rs:41:13
|
||||
|
|
||||
LL | let _ = s1[2..].find(&s2).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1[2..].contains(&s2)`
|
||||
|
||||
error: called `is_none()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_none.rs:42:13
|
||||
|
|
||||
LL | let _ = s1[2..].find(&s2[2..]).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1[2..].contains(&s2[2..])`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:58:25
|
||||
|
|
||||
LL | .filter(|c| filter_hand.iter().find(|cc| c == cc).is_none())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!filter_hand.iter().any(|cc| c == &cc)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:74:30
|
||||
|
|
||||
LL | .filter(|(c, _)| filter_hand.iter().find(|cc| c == *cc).is_none())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!filter_hand.iter().any(|cc| c == cc)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:85:17
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|v| v.foo == 1 && v.bar == 2).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!vfoo.iter().any(|v| v.foo == 1 && v.bar == 2)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:88:17
|
||||
|
|
||||
LL | let _ = vfoo
|
||||
| _________________^
|
||||
LL | | .iter()
|
||||
LL | | .find(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2)
|
||||
LL | | .is_none();
|
||||
| |______________________^
|
||||
|
|
||||
help: use `!_.any()` instead
|
||||
|
|
||||
LL ~ let _ = !vfoo
|
||||
LL ~ .iter().any(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2);
|
||||
|
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:96:17
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|a| a[0] == 42).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!vfoo.iter().any(|a| a[0] == 42)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:102:17
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|sub| sub[1..4].len() == 3).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!vfoo.iter().any(|sub| sub[1..4].len() == 3)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:120:17
|
||||
|
|
||||
LL | let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `![ppx].iter().any(|ppp_x: &&u32| please(ppp_x))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:121:17
|
||||
|
|
||||
LL | let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `![String::from("Hey hey")].iter().any(|s| s.len() == 2)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:124:17
|
||||
|
|
||||
LL | let _ = v.iter().find(|x| deref_enough(**x)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| deref_enough(*x))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:125:17
|
||||
|
|
||||
LL | let _ = v.iter().find(|x: &&u32| deref_enough(**x)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x: &u32| deref_enough(*x))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:128:17
|
||||
|
|
||||
LL | let _ = v.iter().find(|x| arg_no_deref(x)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| arg_no_deref(&x))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:130:17
|
||||
|
|
||||
LL | let _ = v.iter().find(|x: &&u32| arg_no_deref(x)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x: &u32| arg_no_deref(&x))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:150:17
|
||||
|
|
||||
LL | let _ = vfoo
|
||||
| _________________^
|
||||
LL | | .iter()
|
||||
LL | | .find(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2)
|
||||
LL | | .is_none();
|
||||
| |______________________^
|
||||
|
|
||||
help: use `!_.any()` instead
|
||||
|
|
||||
LL ~ let _ = !vfoo
|
||||
LL ~ .iter().any(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2);
|
||||
|
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:166:17
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|v| v.inner[0].bar == 2).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!vfoo.iter().any(|v| v.inner[0].bar == 2)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:171:17
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|x| (**x)[0] == 9).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!vfoo.iter().any(|x| (**x)[0] == 9)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:184:17
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|v| v.by_ref(&v.bar)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!vfoo.iter().any(|v| v.by_ref(&v.bar))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:188:17
|
||||
|
|
||||
LL | let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|(&x, y)| x == *y).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `![&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y)`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:189:17
|
||||
|
|
||||
LL | let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|&(&x, y)| x == *y).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `![&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y)`
|
||||
|
||||
error: writing `&String` instead of `&str` involves a new object where a slice will do
|
||||
--> $DIR/search_is_some_fixable_none.rs:192:25
|
||||
|
|
||||
LL | fn test_string_1(s: &String) -> bool {
|
||||
| ^^^^^^^ help: change this to: `&str`
|
||||
|
|
||||
= note: `-D clippy::ptr-arg` implied by `-D warnings`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:208:17
|
||||
|
|
||||
LL | let _ = v.iter().find(|s| s[0].is_empty()).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|s| s[0].is_empty())`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:209:17
|
||||
|
|
||||
LL | let _ = v.iter().find(|s| test_string_1(&s[0])).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|s| test_string_1(&s[0]))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:218:17
|
||||
|
|
||||
LL | let _ = v.iter().find(|fp| fp.field.is_power_of_two()).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|fp| fp.field.is_power_of_two())`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:219:17
|
||||
|
|
||||
LL | let _ = v.iter().find(|fp| test_u32_1(fp.field)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|fp| test_u32_1(fp.field))`
|
||||
|
||||
error: called `is_none()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_none.rs:220:17
|
||||
|
|
||||
LL | let _ = v.iter().find(|fp| test_u32_2(*fp.field)).is_none();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|fp| test_u32_2(*fp.field))`
|
||||
|
||||
error: aborting due to 44 previous errors
|
||||
|
218
tests/ui/search_is_some_fixable_some.fixed
Normal file
218
tests/ui/search_is_some_fixable_some.fixed
Normal file
@ -0,0 +1,218 @@
|
||||
// run-rustfix
|
||||
#![allow(dead_code)]
|
||||
#![warn(clippy::search_is_some)]
|
||||
|
||||
fn main() {
|
||||
let v = vec![3, 2, 1, 0, -1, -2, -3];
|
||||
let y = &&42;
|
||||
|
||||
// Check `find().is_some()`, single-line case.
|
||||
let _ = v.iter().any(|x| *x < 0);
|
||||
let _ = (0..1).any(|x| **y == x); // one dereference less
|
||||
let _ = (0..1).any(|x| x == 0);
|
||||
let _ = v.iter().any(|x| *x == 0);
|
||||
let _ = (4..5).any(|x| x == 1 || x == 3 || x == 5);
|
||||
let _ = (1..3).any(|x| [1, 2, 3].contains(&x));
|
||||
let _ = (1..3).any(|x| x == 0 || [1, 2, 3].contains(&x));
|
||||
let _ = (1..3).any(|x| [1, 2, 3].contains(&x) || x == 0);
|
||||
let _ = (1..3)
|
||||
.any(|x| [1, 2, 3].contains(&x) || x == 0 || [4, 5, 6].contains(&x) || x == -1);
|
||||
|
||||
// Check `position().is_some()`, single-line case.
|
||||
let _ = v.iter().any(|&x| x < 0);
|
||||
|
||||
// Check `rposition().is_some()`, single-line case.
|
||||
let _ = v.iter().any(|&x| x < 0);
|
||||
|
||||
let s1 = String::from("hello world");
|
||||
let s2 = String::from("world");
|
||||
// caller of `find()` is a `&`static str`
|
||||
let _ = "hello world".contains("world");
|
||||
let _ = "hello world".contains(&s2);
|
||||
let _ = "hello world".contains(&s2[2..]);
|
||||
// caller of `find()` is a `String`
|
||||
let _ = s1.contains("world");
|
||||
let _ = s1.contains(&s2);
|
||||
let _ = s1.contains(&s2[2..]);
|
||||
// caller of `find()` is slice of `String`
|
||||
let _ = s1[2..].contains("world");
|
||||
let _ = s1[2..].contains(&s2);
|
||||
let _ = s1[2..].contains(&s2[2..]);
|
||||
}
|
||||
|
||||
#[allow(clippy::clone_on_copy, clippy::map_clone)]
|
||||
mod issue7392 {
|
||||
struct Player {
|
||||
hand: Vec<usize>,
|
||||
}
|
||||
fn filter() {
|
||||
let p = Player {
|
||||
hand: vec![1, 2, 3, 4, 5],
|
||||
};
|
||||
let filter_hand = vec![5];
|
||||
let _ = p
|
||||
.hand
|
||||
.iter()
|
||||
.filter(|c| filter_hand.iter().any(|cc| c == &cc))
|
||||
.map(|c| c.clone())
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
struct PlayerTuple {
|
||||
hand: Vec<(usize, char)>,
|
||||
}
|
||||
fn filter_tuple() {
|
||||
let p = PlayerTuple {
|
||||
hand: vec![(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')],
|
||||
};
|
||||
let filter_hand = vec![5];
|
||||
let _ = p
|
||||
.hand
|
||||
.iter()
|
||||
.filter(|(c, _)| filter_hand.iter().any(|cc| c == cc))
|
||||
.map(|c| c.clone())
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
fn field_projection() {
|
||||
struct Foo {
|
||||
foo: i32,
|
||||
bar: u32,
|
||||
}
|
||||
let vfoo = vec![Foo { foo: 1, bar: 2 }];
|
||||
let _ = vfoo.iter().any(|v| v.foo == 1 && v.bar == 2);
|
||||
|
||||
let vfoo = vec![(42, Foo { foo: 1, bar: 2 })];
|
||||
let _ = vfoo
|
||||
.iter()
|
||||
.any(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2);
|
||||
}
|
||||
|
||||
fn index_projection() {
|
||||
let vfoo = vec![[0, 1, 2, 3]];
|
||||
let _ = vfoo.iter().any(|a| a[0] == 42);
|
||||
}
|
||||
|
||||
#[allow(clippy::match_like_matches_macro)]
|
||||
fn slice_projection() {
|
||||
let vfoo = vec![[0, 1, 2, 3, 0, 1, 2, 3]];
|
||||
let _ = vfoo.iter().any(|sub| sub[1..4].len() == 3);
|
||||
}
|
||||
|
||||
fn please(x: &u32) -> bool {
|
||||
*x == 9
|
||||
}
|
||||
|
||||
fn deref_enough(x: u32) -> bool {
|
||||
x == 78
|
||||
}
|
||||
|
||||
fn arg_no_deref(x: &&u32) -> bool {
|
||||
**x == 78
|
||||
}
|
||||
|
||||
fn more_projections() {
|
||||
let x = 19;
|
||||
let ppx: &u32 = &x;
|
||||
let _ = [ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
|
||||
let _ = [String::from("Hey hey")].iter().any(|s| s.len() == 2);
|
||||
|
||||
let v = vec![3, 2, 1, 0];
|
||||
let _ = v.iter().any(|x| deref_enough(*x));
|
||||
let _ = v.iter().any(|x: &u32| deref_enough(*x));
|
||||
|
||||
#[allow(clippy::redundant_closure)]
|
||||
let _ = v.iter().any(|x| arg_no_deref(&x));
|
||||
#[allow(clippy::redundant_closure)]
|
||||
let _ = v.iter().any(|x: &u32| arg_no_deref(&x));
|
||||
}
|
||||
|
||||
fn field_index_projection() {
|
||||
struct FooDouble {
|
||||
bar: Vec<Vec<i32>>,
|
||||
}
|
||||
struct Foo {
|
||||
bar: Vec<i32>,
|
||||
}
|
||||
struct FooOuter {
|
||||
inner: Foo,
|
||||
inner_double: FooDouble,
|
||||
}
|
||||
let vfoo = vec![FooOuter {
|
||||
inner: Foo { bar: vec![0, 1, 2, 3] },
|
||||
inner_double: FooDouble {
|
||||
bar: vec![vec![0, 1, 2, 3]],
|
||||
},
|
||||
}];
|
||||
let _ = vfoo
|
||||
.iter()
|
||||
.any(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2);
|
||||
}
|
||||
|
||||
fn index_field_projection() {
|
||||
struct Foo {
|
||||
bar: i32,
|
||||
}
|
||||
struct FooOuter {
|
||||
inner: Vec<Foo>,
|
||||
}
|
||||
let vfoo = vec![FooOuter {
|
||||
inner: vec![Foo { bar: 0 }],
|
||||
}];
|
||||
let _ = vfoo.iter().any(|v| v.inner[0].bar == 2);
|
||||
}
|
||||
|
||||
fn double_deref_index_projection() {
|
||||
let vfoo = vec![&&[0, 1, 2, 3]];
|
||||
let _ = vfoo.iter().any(|x| (**x)[0] == 9);
|
||||
}
|
||||
|
||||
fn method_call_by_ref() {
|
||||
struct Foo {
|
||||
bar: u32,
|
||||
}
|
||||
impl Foo {
|
||||
pub fn by_ref(&self, x: &u32) -> bool {
|
||||
*x == self.bar
|
||||
}
|
||||
}
|
||||
let vfoo = vec![Foo { bar: 1 }];
|
||||
let _ = vfoo.iter().any(|v| v.by_ref(&v.bar));
|
||||
}
|
||||
|
||||
fn ref_bindings() {
|
||||
let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y);
|
||||
let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y);
|
||||
}
|
||||
|
||||
fn test_string_1(s: &str) -> bool {
|
||||
s.is_empty()
|
||||
}
|
||||
|
||||
fn test_u32_1(s: &u32) -> bool {
|
||||
s.is_power_of_two()
|
||||
}
|
||||
|
||||
fn test_u32_2(s: u32) -> bool {
|
||||
s.is_power_of_two()
|
||||
}
|
||||
|
||||
fn projection_in_args_test() {
|
||||
// Index projections
|
||||
let lst = &[String::from("Hello"), String::from("world")];
|
||||
let v: Vec<&[String]> = vec![lst];
|
||||
let _ = v.iter().any(|s| s[0].is_empty());
|
||||
let _ = v.iter().any(|s| test_string_1(&s[0]));
|
||||
|
||||
// Field projections
|
||||
struct FieldProjection<'a> {
|
||||
field: &'a u32,
|
||||
}
|
||||
let field = 123456789;
|
||||
let instance = FieldProjection { field: &field };
|
||||
let v = vec![instance];
|
||||
let _ = v.iter().any(|fp| fp.field.is_power_of_two());
|
||||
let _ = v.iter().any(|fp| test_u32_1(fp.field));
|
||||
let _ = v.iter().any(|fp| test_u32_2(*fp.field));
|
||||
}
|
||||
}
|
221
tests/ui/search_is_some_fixable_some.rs
Normal file
221
tests/ui/search_is_some_fixable_some.rs
Normal file
@ -0,0 +1,221 @@
|
||||
// run-rustfix
|
||||
#![allow(dead_code)]
|
||||
#![warn(clippy::search_is_some)]
|
||||
|
||||
fn main() {
|
||||
let v = vec![3, 2, 1, 0, -1, -2, -3];
|
||||
let y = &&42;
|
||||
|
||||
// Check `find().is_some()`, single-line case.
|
||||
let _ = v.iter().find(|&x| *x < 0).is_some();
|
||||
let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
|
||||
let _ = (0..1).find(|x| *x == 0).is_some();
|
||||
let _ = v.iter().find(|x| **x == 0).is_some();
|
||||
let _ = (4..5).find(|x| *x == 1 || *x == 3 || *x == 5).is_some();
|
||||
let _ = (1..3).find(|x| [1, 2, 3].contains(x)).is_some();
|
||||
let _ = (1..3).find(|x| *x == 0 || [1, 2, 3].contains(x)).is_some();
|
||||
let _ = (1..3).find(|x| [1, 2, 3].contains(x) || *x == 0).is_some();
|
||||
let _ = (1..3)
|
||||
.find(|x| [1, 2, 3].contains(x) || *x == 0 || [4, 5, 6].contains(x) || *x == -1)
|
||||
.is_some();
|
||||
|
||||
// Check `position().is_some()`, single-line case.
|
||||
let _ = v.iter().position(|&x| x < 0).is_some();
|
||||
|
||||
// Check `rposition().is_some()`, single-line case.
|
||||
let _ = v.iter().rposition(|&x| x < 0).is_some();
|
||||
|
||||
let s1 = String::from("hello world");
|
||||
let s2 = String::from("world");
|
||||
// caller of `find()` is a `&`static str`
|
||||
let _ = "hello world".find("world").is_some();
|
||||
let _ = "hello world".find(&s2).is_some();
|
||||
let _ = "hello world".find(&s2[2..]).is_some();
|
||||
// caller of `find()` is a `String`
|
||||
let _ = s1.find("world").is_some();
|
||||
let _ = s1.find(&s2).is_some();
|
||||
let _ = s1.find(&s2[2..]).is_some();
|
||||
// caller of `find()` is slice of `String`
|
||||
let _ = s1[2..].find("world").is_some();
|
||||
let _ = s1[2..].find(&s2).is_some();
|
||||
let _ = s1[2..].find(&s2[2..]).is_some();
|
||||
}
|
||||
|
||||
#[allow(clippy::clone_on_copy, clippy::map_clone)]
|
||||
mod issue7392 {
|
||||
struct Player {
|
||||
hand: Vec<usize>,
|
||||
}
|
||||
fn filter() {
|
||||
let p = Player {
|
||||
hand: vec![1, 2, 3, 4, 5],
|
||||
};
|
||||
let filter_hand = vec![5];
|
||||
let _ = p
|
||||
.hand
|
||||
.iter()
|
||||
.filter(|c| filter_hand.iter().find(|cc| c == cc).is_some())
|
||||
.map(|c| c.clone())
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
struct PlayerTuple {
|
||||
hand: Vec<(usize, char)>,
|
||||
}
|
||||
fn filter_tuple() {
|
||||
let p = PlayerTuple {
|
||||
hand: vec![(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')],
|
||||
};
|
||||
let filter_hand = vec![5];
|
||||
let _ = p
|
||||
.hand
|
||||
.iter()
|
||||
.filter(|(c, _)| filter_hand.iter().find(|cc| c == *cc).is_some())
|
||||
.map(|c| c.clone())
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
fn field_projection() {
|
||||
struct Foo {
|
||||
foo: i32,
|
||||
bar: u32,
|
||||
}
|
||||
let vfoo = vec![Foo { foo: 1, bar: 2 }];
|
||||
let _ = vfoo.iter().find(|v| v.foo == 1 && v.bar == 2).is_some();
|
||||
|
||||
let vfoo = vec![(42, Foo { foo: 1, bar: 2 })];
|
||||
let _ = vfoo
|
||||
.iter()
|
||||
.find(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2)
|
||||
.is_some();
|
||||
}
|
||||
|
||||
fn index_projection() {
|
||||
let vfoo = vec![[0, 1, 2, 3]];
|
||||
let _ = vfoo.iter().find(|a| a[0] == 42).is_some();
|
||||
}
|
||||
|
||||
#[allow(clippy::match_like_matches_macro)]
|
||||
fn slice_projection() {
|
||||
let vfoo = vec![[0, 1, 2, 3, 0, 1, 2, 3]];
|
||||
let _ = vfoo.iter().find(|sub| sub[1..4].len() == 3).is_some();
|
||||
}
|
||||
|
||||
fn please(x: &u32) -> bool {
|
||||
*x == 9
|
||||
}
|
||||
|
||||
fn deref_enough(x: u32) -> bool {
|
||||
x == 78
|
||||
}
|
||||
|
||||
fn arg_no_deref(x: &&u32) -> bool {
|
||||
**x == 78
|
||||
}
|
||||
|
||||
fn more_projections() {
|
||||
let x = 19;
|
||||
let ppx: &u32 = &x;
|
||||
let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_some();
|
||||
let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_some();
|
||||
|
||||
let v = vec![3, 2, 1, 0];
|
||||
let _ = v.iter().find(|x| deref_enough(**x)).is_some();
|
||||
let _ = v.iter().find(|x: &&u32| deref_enough(**x)).is_some();
|
||||
|
||||
#[allow(clippy::redundant_closure)]
|
||||
let _ = v.iter().find(|x| arg_no_deref(x)).is_some();
|
||||
#[allow(clippy::redundant_closure)]
|
||||
let _ = v.iter().find(|x: &&u32| arg_no_deref(x)).is_some();
|
||||
}
|
||||
|
||||
fn field_index_projection() {
|
||||
struct FooDouble {
|
||||
bar: Vec<Vec<i32>>,
|
||||
}
|
||||
struct Foo {
|
||||
bar: Vec<i32>,
|
||||
}
|
||||
struct FooOuter {
|
||||
inner: Foo,
|
||||
inner_double: FooDouble,
|
||||
}
|
||||
let vfoo = vec![FooOuter {
|
||||
inner: Foo { bar: vec![0, 1, 2, 3] },
|
||||
inner_double: FooDouble {
|
||||
bar: vec![vec![0, 1, 2, 3]],
|
||||
},
|
||||
}];
|
||||
let _ = vfoo
|
||||
.iter()
|
||||
.find(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2)
|
||||
.is_some();
|
||||
}
|
||||
|
||||
fn index_field_projection() {
|
||||
struct Foo {
|
||||
bar: i32,
|
||||
}
|
||||
struct FooOuter {
|
||||
inner: Vec<Foo>,
|
||||
}
|
||||
let vfoo = vec![FooOuter {
|
||||
inner: vec![Foo { bar: 0 }],
|
||||
}];
|
||||
let _ = vfoo.iter().find(|v| v.inner[0].bar == 2).is_some();
|
||||
}
|
||||
|
||||
fn double_deref_index_projection() {
|
||||
let vfoo = vec![&&[0, 1, 2, 3]];
|
||||
let _ = vfoo.iter().find(|x| (**x)[0] == 9).is_some();
|
||||
}
|
||||
|
||||
fn method_call_by_ref() {
|
||||
struct Foo {
|
||||
bar: u32,
|
||||
}
|
||||
impl Foo {
|
||||
pub fn by_ref(&self, x: &u32) -> bool {
|
||||
*x == self.bar
|
||||
}
|
||||
}
|
||||
let vfoo = vec![Foo { bar: 1 }];
|
||||
let _ = vfoo.iter().find(|v| v.by_ref(&v.bar)).is_some();
|
||||
}
|
||||
|
||||
fn ref_bindings() {
|
||||
let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|(&x, y)| x == *y).is_some();
|
||||
let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|&(&x, y)| x == *y).is_some();
|
||||
}
|
||||
|
||||
fn test_string_1(s: &String) -> bool {
|
||||
s.is_empty()
|
||||
}
|
||||
|
||||
fn test_u32_1(s: &u32) -> bool {
|
||||
s.is_power_of_two()
|
||||
}
|
||||
|
||||
fn test_u32_2(s: u32) -> bool {
|
||||
s.is_power_of_two()
|
||||
}
|
||||
|
||||
fn projection_in_args_test() {
|
||||
// Index projections
|
||||
let lst = &[String::from("Hello"), String::from("world")];
|
||||
let v: Vec<&[String]> = vec![lst];
|
||||
let _ = v.iter().find(|s| s[0].is_empty()).is_some();
|
||||
let _ = v.iter().find(|s| test_string_1(&s[0])).is_some();
|
||||
|
||||
// Field projections
|
||||
struct FieldProjection<'a> {
|
||||
field: &'a u32,
|
||||
}
|
||||
let field = 123456789;
|
||||
let instance = FieldProjection { field: &field };
|
||||
let v = vec![instance];
|
||||
let _ = v.iter().find(|fp| fp.field.is_power_of_two()).is_some();
|
||||
let _ = v.iter().find(|fp| test_u32_1(fp.field)).is_some();
|
||||
let _ = v.iter().find(|fp| test_u32_2(*fp.field)).is_some();
|
||||
}
|
||||
}
|
276
tests/ui/search_is_some_fixable_some.stderr
Normal file
276
tests/ui/search_is_some_fixable_some.stderr
Normal file
@ -0,0 +1,276 @@
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:10:22
|
||||
|
|
||||
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| *x < 0)`
|
||||
|
|
||||
= note: `-D clippy::search-is-some` implied by `-D warnings`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:11:20
|
||||
|
|
||||
LL | let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| **y == x)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:12:20
|
||||
|
|
||||
LL | let _ = (0..1).find(|x| *x == 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| x == 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:13:22
|
||||
|
|
||||
LL | let _ = v.iter().find(|x| **x == 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| *x == 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:14:20
|
||||
|
|
||||
LL | let _ = (4..5).find(|x| *x == 1 || *x == 3 || *x == 5).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| x == 1 || x == 3 || x == 5)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:15:20
|
||||
|
|
||||
LL | let _ = (1..3).find(|x| [1, 2, 3].contains(x)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| [1, 2, 3].contains(&x))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:16:20
|
||||
|
|
||||
LL | let _ = (1..3).find(|x| *x == 0 || [1, 2, 3].contains(x)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| x == 0 || [1, 2, 3].contains(&x))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:17:20
|
||||
|
|
||||
LL | let _ = (1..3).find(|x| [1, 2, 3].contains(x) || *x == 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| [1, 2, 3].contains(&x) || x == 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:19:10
|
||||
|
|
||||
LL | .find(|x| [1, 2, 3].contains(x) || *x == 0 || [4, 5, 6].contains(x) || *x == -1)
|
||||
| __________^
|
||||
LL | | .is_some();
|
||||
| |__________________^ help: use `any()` instead: `any(|x| [1, 2, 3].contains(&x) || x == 0 || [4, 5, 6].contains(&x) || x == -1)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `position`
|
||||
--> $DIR/search_is_some_fixable_some.rs:23:22
|
||||
|
|
||||
LL | let _ = v.iter().position(|&x| x < 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|&x| x < 0)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `rposition`
|
||||
--> $DIR/search_is_some_fixable_some.rs:26:22
|
||||
|
|
||||
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|&x| x < 0)`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_some.rs:31:27
|
||||
|
|
||||
LL | let _ = "hello world".find("world").is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains("world")`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_some.rs:32:27
|
||||
|
|
||||
LL | let _ = "hello world".find(&s2).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2)`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_some.rs:33:27
|
||||
|
|
||||
LL | let _ = "hello world".find(&s2[2..]).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_some.rs:35:16
|
||||
|
|
||||
LL | let _ = s1.find("world").is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains("world")`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_some.rs:36:16
|
||||
|
|
||||
LL | let _ = s1.find(&s2).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2)`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_some.rs:37:16
|
||||
|
|
||||
LL | let _ = s1.find(&s2[2..]).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_some.rs:39:21
|
||||
|
|
||||
LL | let _ = s1[2..].find("world").is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains("world")`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_some.rs:40:21
|
||||
|
|
||||
LL | let _ = s1[2..].find(&s2).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2)`
|
||||
|
||||
error: called `is_some()` after calling `find()` on a string
|
||||
--> $DIR/search_is_some_fixable_some.rs:41:21
|
||||
|
|
||||
LL | let _ = s1[2..].find(&s2[2..]).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:57:44
|
||||
|
|
||||
LL | .filter(|c| filter_hand.iter().find(|cc| c == cc).is_some())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|cc| c == &cc)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:73:49
|
||||
|
|
||||
LL | .filter(|(c, _)| filter_hand.iter().find(|cc| c == *cc).is_some())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|cc| c == cc)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:84:29
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|v| v.foo == 1 && v.bar == 2).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|v| v.foo == 1 && v.bar == 2)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:89:14
|
||||
|
|
||||
LL | .find(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2)
|
||||
| ______________^
|
||||
LL | | .is_some();
|
||||
| |______________________^ help: use `any()` instead: `any(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:95:29
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|a| a[0] == 42).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|a| a[0] == 42)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:101:29
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|sub| sub[1..4].len() == 3).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|sub| sub[1..4].len() == 3)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:119:30
|
||||
|
|
||||
LL | let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|ppp_x: &&u32| please(ppp_x))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:120:50
|
||||
|
|
||||
LL | let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|s| s.len() == 2)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:123:26
|
||||
|
|
||||
LL | let _ = v.iter().find(|x| deref_enough(**x)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| deref_enough(*x))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:124:26
|
||||
|
|
||||
LL | let _ = v.iter().find(|x: &&u32| deref_enough(**x)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x: &u32| deref_enough(*x))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:127:26
|
||||
|
|
||||
LL | let _ = v.iter().find(|x| arg_no_deref(x)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| arg_no_deref(&x))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:129:26
|
||||
|
|
||||
LL | let _ = v.iter().find(|x: &&u32| arg_no_deref(x)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x: &u32| arg_no_deref(&x))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:151:14
|
||||
|
|
||||
LL | .find(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2)
|
||||
| ______________^
|
||||
LL | | .is_some();
|
||||
| |______________________^ help: use `any()` instead: `any(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:165:29
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|v| v.inner[0].bar == 2).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|v| v.inner[0].bar == 2)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:170:29
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|x| (**x)[0] == 9).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| (**x)[0] == 9)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:183:29
|
||||
|
|
||||
LL | let _ = vfoo.iter().find(|v| v.by_ref(&v.bar)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|v| v.by_ref(&v.bar))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:187:55
|
||||
|
|
||||
LL | let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|(&x, y)| x == *y).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|(&x, y)| x == *y)`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:188:55
|
||||
|
|
||||
LL | let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|&(&x, y)| x == *y).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|(&x, y)| x == *y)`
|
||||
|
||||
error: writing `&String` instead of `&str` involves a new object where a slice will do
|
||||
--> $DIR/search_is_some_fixable_some.rs:191:25
|
||||
|
|
||||
LL | fn test_string_1(s: &String) -> bool {
|
||||
| ^^^^^^^ help: change this to: `&str`
|
||||
|
|
||||
= note: `-D clippy::ptr-arg` implied by `-D warnings`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:207:26
|
||||
|
|
||||
LL | let _ = v.iter().find(|s| s[0].is_empty()).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|s| s[0].is_empty())`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:208:26
|
||||
|
|
||||
LL | let _ = v.iter().find(|s| test_string_1(&s[0])).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|s| test_string_1(&s[0]))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:217:26
|
||||
|
|
||||
LL | let _ = v.iter().find(|fp| fp.field.is_power_of_two()).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|fp| fp.field.is_power_of_two())`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:218:26
|
||||
|
|
||||
LL | let _ = v.iter().find(|fp| test_u32_1(fp.field)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|fp| test_u32_1(fp.field))`
|
||||
|
||||
error: called `is_some()` after searching an `Iterator` with `find`
|
||||
--> $DIR/search_is_some_fixable_some.rs:219:26
|
||||
|
|
||||
LL | let _ = v.iter().find(|fp| test_u32_2(*fp.field)).is_some();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|fp| test_u32_2(*fp.field))`
|
||||
|
||||
error: aborting due to 44 previous errors
|
||||
|
Loading…
Reference in New Issue
Block a user