rust/tests/ui/iter_on_single_items.rs

64 lines
1.7 KiB
Rust
Raw Normal View History

2022-07-16 08:22:43 +00:00
// run-rustfix
#![warn(clippy::iter_on_single_items)]
2022-07-16 08:22:43 +00:00
#![allow(clippy::iter_next_slice, clippy::redundant_clone)]
fn array() {
assert_eq!([123].into_iter().next(), Some(123));
assert_eq!([123].iter_mut().next(), Some(&mut 123));
assert_eq!([123].iter().next(), Some(&123));
assert_eq!(Some(123).into_iter().next(), Some(123));
assert_eq!(Some(123).iter_mut().next(), Some(&mut 123));
assert_eq!(Some(123).iter().next(), Some(&123));
// Don't trigger on non-iter methods
let _: Option<String> = Some("test".to_string()).clone();
let _: [String; 1] = ["test".to_string()].clone();
2022-07-28 20:54:23 +00:00
// Don't trigger on match or if branches
let _ = match 123 {
123 => [].iter(),
_ => ["test"].iter(),
};
let _ = if false { ["test"].iter() } else { [].iter() };
2022-07-16 08:22:43 +00:00
}
macro_rules! in_macros {
() => {
assert_eq!([123].into_iter().next(), Some(123));
assert_eq!([123].iter_mut().next(), Some(&mut 123));
assert_eq!([123].iter().next(), Some(&123));
assert_eq!(Some(123).into_iter().next(), Some(123));
assert_eq!(Some(123).iter_mut().next(), Some(&mut 123));
assert_eq!(Some(123).iter().next(), Some(&123));
};
}
// Don't trigger on a `Some` that isn't std's option
mod custom_option {
#[allow(unused)]
enum CustomOption {
Some(i32),
None,
}
impl CustomOption {
fn iter(&self) {}
fn iter_mut(&mut self) {}
fn into_iter(self) {}
}
use CustomOption::*;
pub fn custom_option() {
Some(3).iter();
Some(3).iter_mut();
Some(3).into_iter();
}
}
2022-07-16 08:22:43 +00:00
fn main() {
array();
custom_option::custom_option();
2022-07-16 08:22:43 +00:00
in_macros!();
}