Auto merge of #8649 - ebobrow:imperative_find, r=flip1995

add [`manual_find`] lint for function return case

part of the implementation discussed in #7143

changelog: add [`manual_find`] lint for function return case
This commit is contained in:
bors 2022-06-27 17:15:25 +00:00
commit fdd0e727e2
15 changed files with 844 additions and 31 deletions

22
tests/ui/manual_find.rs Normal file
View file

@ -0,0 +1,22 @@
#![allow(unused)]
#![warn(clippy::manual_find)]
fn vec_string(strings: Vec<String>) -> Option<String> {
for s in strings {
if s == String::new() {
return Some(s);
}
}
None
}
fn tuple(arr: Vec<(String, i32)>) -> Option<String> {
for (s, _) in arr {
if s == String::new() {
return Some(s);
}
}
None
}
fn main() {}

View file

@ -0,0 +1,29 @@
error: manual implementation of `Iterator::find`
--> $DIR/manual_find.rs:5:5
|
LL | / for s in strings {
LL | | if s == String::new() {
LL | | return Some(s);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `strings.into_iter().find(|s| s == String::new())`
|
= note: `-D clippy::manual-find` implied by `-D warnings`
= note: you may need to dereference some variables
error: manual implementation of `Iterator::find`
--> $DIR/manual_find.rs:14:5
|
LL | / for (s, _) in arr {
LL | | if s == String::new() {
LL | | return Some(s);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `arr.into_iter().map(|(s, _)| s).find(|s| s == String::new())`
|
= note: you may need to dereference some variables
error: aborting due to 2 previous errors

View file

@ -0,0 +1,182 @@
// run-rustfix
#![allow(unused, clippy::needless_return)]
#![warn(clippy::manual_find)]
use std::collections::HashMap;
const ARRAY: &[u32; 5] = &[2, 7, 1, 9, 3];
fn lookup(n: u32) -> Option<u32> {
ARRAY.iter().find(|&&v| v == n).copied()
}
fn with_pat(arr: Vec<(u32, u32)>) -> Option<u32> {
arr.into_iter().map(|(a, _)| a).find(|&a| a % 2 == 0)
}
struct Data {
name: String,
is_true: bool,
}
fn with_struct(arr: Vec<Data>) -> Option<Data> {
arr.into_iter().find(|el| el.name.len() == 10)
}
struct Tuple(usize, usize);
fn with_tuple_struct(arr: Vec<Tuple>) -> Option<usize> {
arr.into_iter().map(|Tuple(a, _)| a).find(|&a| a >= 3)
}
struct A;
impl A {
fn should_keep(&self) -> bool {
true
}
}
fn with_method_call(arr: Vec<A>) -> Option<A> {
arr.into_iter().find(|el| el.should_keep())
}
fn with_closure(arr: Vec<u32>) -> Option<u32> {
let f = |el: u32| -> u32 { el + 10 };
arr.into_iter().find(|&el| f(el) == 20)
}
fn with_closure2(arr: HashMap<String, i32>) -> Option<i32> {
let f = |el: i32| -> bool { el == 10 };
arr.values().find(|&&el| f(el)).copied()
}
fn with_bool(arr: Vec<Data>) -> Option<Data> {
arr.into_iter().find(|el| el.is_true)
}
fn with_side_effects(arr: Vec<u32>) -> Option<u32> {
for v in arr {
if v == 1 {
println!("side effect");
return Some(v);
}
}
None
}
fn with_else(arr: Vec<u32>) -> Option<u32> {
for el in arr {
if el % 2 == 0 {
return Some(el);
} else {
println!("{}", el);
}
}
None
}
fn tuple_with_ref(v: [(u8, &u8); 3]) -> Option<u8> {
v.into_iter().map(|(_, &x)| x).find(|&x| x > 10)
}
fn ref_to_tuple_with_ref(v: &[(u8, &u8)]) -> Option<u8> {
v.iter().map(|&(_, &x)| x).find(|&x| x > 10)
}
fn explicit_ret(arr: Vec<i32>) -> Option<i32> {
arr.into_iter().find(|&x| x >= 5)
}
fn plus_one(a: i32) -> Option<i32> {
Some(a + 1)
}
fn fn_instead_of_some(a: &[i32]) -> Option<i32> {
for &x in a {
if x == 1 {
return plus_one(x);
}
}
None
}
fn for_in_condition(a: &[i32], b: bool) -> Option<i32> {
if b {
for &x in a {
if x == 1 {
return Some(x);
}
}
}
None
}
fn intermediate_statements(a: &[i32]) -> Option<i32> {
for &x in a {
if x == 1 {
return Some(x);
}
}
println!("side effect");
None
}
fn mixed_binding_modes(arr: Vec<(i32, String)>) -> Option<i32> {
for (x, mut s) in arr {
if x == 1 && s.as_mut_str().len() == 2 {
return Some(x);
}
}
None
}
fn as_closure() {
#[rustfmt::skip]
let f = |arr: Vec<i32>| -> Option<i32> {
arr.into_iter().find(|&x| x < 1)
};
}
fn in_block(a: &[i32]) -> Option<i32> {
let should_be_none = {
for &x in a {
if x == 1 {
return Some(x);
}
}
None
};
assert!(should_be_none.is_none());
should_be_none
}
// Not handled yet
fn mut_binding(v: Vec<String>) -> Option<String> {
for mut s in v {
if s.as_mut_str().len() > 1 {
return Some(s);
}
}
None
}
fn subpattern(v: Vec<[u32; 32]>) -> Option<[u32; 32]> {
for a @ [first, ..] in v {
if a[12] == first {
return Some(a);
}
}
None
}
fn two_bindings(v: Vec<(u8, u8)>) -> Option<u8> {
for (a, n) in v {
if a == n {
return Some(a);
}
}
None
}
fn main() {}

View file

@ -0,0 +1,242 @@
// run-rustfix
#![allow(unused, clippy::needless_return)]
#![warn(clippy::manual_find)]
use std::collections::HashMap;
const ARRAY: &[u32; 5] = &[2, 7, 1, 9, 3];
fn lookup(n: u32) -> Option<u32> {
for &v in ARRAY {
if v == n {
return Some(v);
}
}
None
}
fn with_pat(arr: Vec<(u32, u32)>) -> Option<u32> {
for (a, _) in arr {
if a % 2 == 0 {
return Some(a);
}
}
None
}
struct Data {
name: String,
is_true: bool,
}
fn with_struct(arr: Vec<Data>) -> Option<Data> {
for el in arr {
if el.name.len() == 10 {
return Some(el);
}
}
None
}
struct Tuple(usize, usize);
fn with_tuple_struct(arr: Vec<Tuple>) -> Option<usize> {
for Tuple(a, _) in arr {
if a >= 3 {
return Some(a);
}
}
None
}
struct A;
impl A {
fn should_keep(&self) -> bool {
true
}
}
fn with_method_call(arr: Vec<A>) -> Option<A> {
for el in arr {
if el.should_keep() {
return Some(el);
}
}
None
}
fn with_closure(arr: Vec<u32>) -> Option<u32> {
let f = |el: u32| -> u32 { el + 10 };
for el in arr {
if f(el) == 20 {
return Some(el);
}
}
None
}
fn with_closure2(arr: HashMap<String, i32>) -> Option<i32> {
let f = |el: i32| -> bool { el == 10 };
for &el in arr.values() {
if f(el) {
return Some(el);
}
}
None
}
fn with_bool(arr: Vec<Data>) -> Option<Data> {
for el in arr {
if el.is_true {
return Some(el);
}
}
None
}
fn with_side_effects(arr: Vec<u32>) -> Option<u32> {
for v in arr {
if v == 1 {
println!("side effect");
return Some(v);
}
}
None
}
fn with_else(arr: Vec<u32>) -> Option<u32> {
for el in arr {
if el % 2 == 0 {
return Some(el);
} else {
println!("{}", el);
}
}
None
}
fn tuple_with_ref(v: [(u8, &u8); 3]) -> Option<u8> {
for (_, &x) in v {
if x > 10 {
return Some(x);
}
}
None
}
fn ref_to_tuple_with_ref(v: &[(u8, &u8)]) -> Option<u8> {
for &(_, &x) in v {
if x > 10 {
return Some(x);
}
}
None
}
fn explicit_ret(arr: Vec<i32>) -> Option<i32> {
for x in arr {
if x >= 5 {
return Some(x);
}
}
return None;
}
fn plus_one(a: i32) -> Option<i32> {
Some(a + 1)
}
fn fn_instead_of_some(a: &[i32]) -> Option<i32> {
for &x in a {
if x == 1 {
return plus_one(x);
}
}
None
}
fn for_in_condition(a: &[i32], b: bool) -> Option<i32> {
if b {
for &x in a {
if x == 1 {
return Some(x);
}
}
}
None
}
fn intermediate_statements(a: &[i32]) -> Option<i32> {
for &x in a {
if x == 1 {
return Some(x);
}
}
println!("side effect");
None
}
fn mixed_binding_modes(arr: Vec<(i32, String)>) -> Option<i32> {
for (x, mut s) in arr {
if x == 1 && s.as_mut_str().len() == 2 {
return Some(x);
}
}
None
}
fn as_closure() {
#[rustfmt::skip]
let f = |arr: Vec<i32>| -> Option<i32> {
for x in arr {
if x < 1 {
return Some(x);
}
}
None
};
}
fn in_block(a: &[i32]) -> Option<i32> {
let should_be_none = {
for &x in a {
if x == 1 {
return Some(x);
}
}
None
};
assert!(should_be_none.is_none());
should_be_none
}
// Not handled yet
fn mut_binding(v: Vec<String>) -> Option<String> {
for mut s in v {
if s.as_mut_str().len() > 1 {
return Some(s);
}
}
None
}
fn subpattern(v: Vec<[u32; 32]>) -> Option<[u32; 32]> {
for a @ [first, ..] in v {
if a[12] == first {
return Some(a);
}
}
None
}
fn two_bindings(v: Vec<(u8, u8)>) -> Option<u8> {
for (a, n) in v {
if a == n {
return Some(a);
}
}
None
}
fn main() {}

View file

@ -0,0 +1,142 @@
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:11:5
|
LL | / for &v in ARRAY {
LL | | if v == n {
LL | | return Some(v);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `ARRAY.iter().find(|&&v| v == n).copied()`
|
= note: `-D clippy::manual-find` implied by `-D warnings`
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:20:5
|
LL | / for (a, _) in arr {
LL | | if a % 2 == 0 {
LL | | return Some(a);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `arr.into_iter().map(|(a, _)| a).find(|&a| a % 2 == 0)`
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:33:5
|
LL | / for el in arr {
LL | | if el.name.len() == 10 {
LL | | return Some(el);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `arr.into_iter().find(|el| el.name.len() == 10)`
|
= note: you may need to dereference some variables
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:43:5
|
LL | / for Tuple(a, _) in arr {
LL | | if a >= 3 {
LL | | return Some(a);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `arr.into_iter().map(|Tuple(a, _)| a).find(|&a| a >= 3)`
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:58:5
|
LL | / for el in arr {
LL | | if el.should_keep() {
LL | | return Some(el);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `arr.into_iter().find(|el| el.should_keep())`
|
= note: you may need to dereference some variables
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:68:5
|
LL | / for el in arr {
LL | | if f(el) == 20 {
LL | | return Some(el);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `arr.into_iter().find(|&el| f(el) == 20)`
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:78:5
|
LL | / for &el in arr.values() {
LL | | if f(el) {
LL | | return Some(el);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `arr.values().find(|&&el| f(el)).copied()`
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:87:5
|
LL | / for el in arr {
LL | | if el.is_true {
LL | | return Some(el);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `arr.into_iter().find(|el| el.is_true)`
|
= note: you may need to dereference some variables
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:117:5
|
LL | / for (_, &x) in v {
LL | | if x > 10 {
LL | | return Some(x);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `v.into_iter().map(|(_, &x)| x).find(|&x| x > 10)`
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:126:5
|
LL | / for &(_, &x) in v {
LL | | if x > 10 {
LL | | return Some(x);
LL | | }
LL | | }
LL | | None
| |________^ help: replace with an iterator: `v.iter().map(|&(_, &x)| x).find(|&x| x > 10)`
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:135:5
|
LL | / for x in arr {
LL | | if x >= 5 {
LL | | return Some(x);
LL | | }
LL | | }
LL | | return None;
| |________________^ help: replace with an iterator: `arr.into_iter().find(|&x| x >= 5)`
error: manual implementation of `Iterator::find`
--> $DIR/manual_find_fixable.rs:190:9
|
LL | / for x in arr {
LL | | if x < 1 {
LL | | return Some(x);
LL | | }
LL | | }
LL | | None
| |____________^ help: replace with an iterator: `arr.into_iter().find(|&x| x < 1)`
error: aborting due to 12 previous errors

View file

@ -6,7 +6,8 @@
unreachable_code,
unused_mut,
dead_code,
clippy::equatable_if_let
clippy::equatable_if_let,
clippy::manual_find
)]
fn base() {

View file

@ -6,7 +6,8 @@
unreachable_code,
unused_mut,
dead_code,
clippy::equatable_if_let
clippy::equatable_if_let,
clippy::manual_find
)]
fn base() {

View file

@ -1,5 +1,5 @@
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:14:5
--> $DIR/while_let_on_iterator.rs:15:5
|
LL | while let Option::Some(x) = iter.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in iter`
@ -7,85 +7,85 @@ LL | while let Option::Some(x) = iter.next() {
= note: `-D clippy::while-let-on-iterator` implied by `-D warnings`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:19:5
--> $DIR/while_let_on_iterator.rs:20:5
|
LL | while let Some(x) = iter.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in iter`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:24:5
--> $DIR/while_let_on_iterator.rs:25:5
|
LL | while let Some(_) = iter.next() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in iter`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:100:9
--> $DIR/while_let_on_iterator.rs:101:9
|
LL | while let Some([..]) = it.next() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for [..] in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:107:9
--> $DIR/while_let_on_iterator.rs:108:9
|
LL | while let Some([_x]) = it.next() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for [_x] in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:120:9
--> $DIR/while_let_on_iterator.rs:121:9
|
LL | while let Some(x @ [_]) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x @ [_] in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:140:9
--> $DIR/while_let_on_iterator.rs:141:9
|
LL | while let Some(_) = y.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in y`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:197:9
--> $DIR/while_let_on_iterator.rs:198:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:208:5
--> $DIR/while_let_on_iterator.rs:209:5
|
LL | while let Some(n) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for n in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:210:9
--> $DIR/while_let_on_iterator.rs:211:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:219:9
--> $DIR/while_let_on_iterator.rs:220:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:228:9
--> $DIR/while_let_on_iterator.rs:229:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:245:9
--> $DIR/while_let_on_iterator.rs:246:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:260:13
--> $DIR/while_let_on_iterator.rs:261:13
|
LL | while let Some(i) = self.0.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for i in self.0.by_ref()`
error: manual `!RangeInclusive::contains` implementation
--> $DIR/while_let_on_iterator.rs:261:20
--> $DIR/while_let_on_iterator.rs:262:20
|
LL | if i < 3 || i > 7 {
| ^^^^^^^^^^^^^^ help: use: `!(3..=7).contains(&i)`
@ -93,49 +93,49 @@ LL | if i < 3 || i > 7 {
= note: `-D clippy::manual-range-contains` implied by `-D warnings`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:292:13
--> $DIR/while_let_on_iterator.rs:293:13
|
LL | while let Some(i) = self.0.0.0.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for i in self.0.0.0.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:321:5
--> $DIR/while_let_on_iterator.rs:322:5
|
LL | while let Some(n) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for n in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:333:9
--> $DIR/while_let_on_iterator.rs:334:9
|
LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:347:5
--> $DIR/while_let_on_iterator.rs:348:5
|
LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:358:5
--> $DIR/while_let_on_iterator.rs:359:5
|
LL | while let Some(x) = it.0.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it.0.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:393:5
--> $DIR/while_let_on_iterator.rs:394:5
|
LL | while let Some(x) = s.x.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in s.x.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:400:5
--> $DIR/while_let_on_iterator.rs:401:5
|
LL | while let Some(x) = x[0].next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in x[0].by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:407:5
--> $DIR/while_let_on_iterator.rs:408:5
|
LL | while let Some(..) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in it`