]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/for_loop_fixable.rs
iterate List by value
[rust.git] / tests / ui / for_loop_fixable.rs
index 2f42ea3ca417e280f9b3ec2b8e73e6093035e899..306d85a6351e107f6a7e6bbb0b8c70d9843a4a93 100644 (file)
@@ -21,73 +21,18 @@ fn iter(&self) -> std::slice::Iter<u8> {
     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
@@ -299,3 +244,40 @@ pub fn test<H: Handle>() -> H {
         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() {}
+    }
+}