clippy::explicit_iter_loop,
clippy::explicit_into_iter_loop,
clippy::iter_next_loop,
- clippy::reverse_range_loop,
clippy::for_kv_map
)]
#[allow(
clippy::linkedlist,
clippy::shadow_unrelated,
clippy::unnecessary_mut_passed,
- clippy::cognitive_complexity,
clippy::similar_names
)]
#[allow(clippy::many_single_char_names, unused_variables)]
fn main() {
- const MAX_LEN: usize = 42;
let mut vec = vec![1, 2, 3, 4];
- for i in 10..0 {
- println!("{}", i);
- }
-
- for i in 10..=0 {
- println!("{}", i);
- }
-
- for i in MAX_LEN..0 {
- println!("{}", i);
- }
-
- for i in 5..=5 {
- // not an error, this is the range with only one element “5”
- println!("{}", i);
- }
-
- for i in 0..10 {
- // not an error, the start index is less than the end index
- println!("{}", i);
- }
-
- for i in -10..0 {
- // not an error
- println!("{}", i);
- }
-
- for i in (10..0).map(|x| x * 2) {
- // not an error, it can't be known what arbitrary methods do to a range
- println!("{}", i);
- }
-
- // testing that the empty range lint folds constants
- for i in 10..5 + 4 {
- println!("{}", i);
- }
-
- for i in (5 + 2)..(3 - 1) {
- println!("{}", i);
- }
-
- for i in (2 * 2)..(2 * 3) {
- // no error, 4..6 is fine
- println!("{}", i);
- }
-
- let x = 42;
- for i in x..10 {
- // no error, not constant-foldable
- println!("{}", i);
- }
-
// See #601
for i in 0..10 {
// no error, id_col does not exist outside the loop
unimplemented!()
}
}
+
+// explicit_into_iter_loop bad suggestions
+#[warn(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop)]
+mod issue_4958 {
+ fn takes_iterator<T>(iterator: &T)
+ where
+ for<'a> &'a T: IntoIterator<Item = &'a String>,
+ {
+ for i in iterator.into_iter() {
+ println!("{}", i);
+ }
+ }
+
+ struct T;
+ impl IntoIterator for &T {
+ type Item = ();
+ type IntoIter = std::vec::IntoIter<Self::Item>;
+ fn into_iter(self) -> Self::IntoIter {
+ vec![].into_iter()
+ }
+ }
+
+ fn more_tests() {
+ let t = T;
+ let r = &t;
+ let rr = &&t;
+
+ // This case is handled by `explicit_iter_loop`. No idea why.
+ for _ in t.into_iter() {}
+
+ for _ in r.into_iter() {}
+
+ // No suggestion for this.
+ // We'd have to suggest `for _ in *rr {}` which is less clear.
+ for _ in rr.into_iter() {}
+ }
+}