rust/tests/ui/search_is_some_fixable_some.fixed

77 lines
2.3 KiB
Rust
Raw Normal View History

// 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()
2021-08-19 08:38:40 +00:00
.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()
2021-08-19 08:38:40 +00:00
.filter(|(c, _)| filter_hand.iter().any(|cc| c == cc))
.map(|c| c.clone())
.collect::<Vec<_>>();
}
}