]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/unnecessary_clone.rs
iterate List by value
[rust.git] / tests / ui / unnecessary_clone.rs
index df02570d692c118b4ce05b6d10eb34c974b188c4..f1cc5b564c1dca66329e21bf9b3c02cc22c7ce4d 100644 (file)
@@ -1,20 +1,9 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-
-
+// does not test any rustfixable lints
 
 #![warn(clippy::clone_on_ref_ptr)]
-#![allow(unused)]
+#![allow(unused, clippy::redundant_clone)]
 
-use std::collections::HashSet;
-use std::collections::VecDeque;
+use std::cell::RefCell;
 use std::rc::{self, Rc};
 use std::sync::{self, Arc};
 
@@ -24,12 +13,29 @@ impl SomeTrait for SomeImpl {}
 
 fn main() {}
 
+fn is_ascii(ch: char) -> bool {
+    ch.is_ascii()
+}
+
 fn clone_on_copy() {
     42.clone();
 
     vec![1].clone(); // ok, not a Copy type
     Some(vec![1]).clone(); // ok, not a Copy type
     (&42).clone();
+
+    let rc = RefCell::new(0);
+    rc.borrow().clone();
+
+    // Issue #4348
+    let mut x = 43;
+    let _ = &x.clone(); // ok, getting a ref
+    'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
+    is_ascii('z'.clone());
+
+    // Issue #5436
+    let mut vec = Vec::new();
+    vec.push(42.clone());
 }
 
 fn clone_on_ref_ptr() {
@@ -52,7 +58,7 @@ fn clone_on_ref_ptr() {
     sync::Weak::clone(&arc_weak);
 
     let x = Arc::new(SomeImpl);
-    let _: Arc<SomeTrait> = x.clone();
+    let _: Arc<dyn SomeTrait> = x.clone();
 }
 
 fn clone_on_copy_generic<T: Copy>(t: T) {
@@ -66,12 +72,46 @@ fn clone_on_double_ref() {
     let y = &&x;
     let z: &Vec<_> = y.clone();
 
-    println!("{:p} {:p}",*y, z);
+    println!("{:p} {:p}", *y, z);
 }
 
-fn iter_clone_collect() {
-    let v = [1,2,3,4,5];
-    let v2 : Vec<isize> = v.iter().cloned().collect();
-    let v3 : HashSet<isize> = v.iter().cloned().collect();
-    let v4 : VecDeque<isize> = v.iter().cloned().collect();
+mod many_derefs {
+    struct A;
+    struct B;
+    struct C;
+    struct D;
+    #[derive(Copy, Clone)]
+    struct E;
+
+    macro_rules! impl_deref {
+        ($src:ident, $dst:ident) => {
+            impl std::ops::Deref for $src {
+                type Target = $dst;
+                fn deref(&self) -> &Self::Target {
+                    &$dst
+                }
+            }
+        };
+    }
+
+    impl_deref!(A, B);
+    impl_deref!(B, C);
+    impl_deref!(C, D);
+    impl std::ops::Deref for D {
+        type Target = &'static E;
+        fn deref(&self) -> &Self::Target {
+            &&E
+        }
+    }
+
+    fn go1() {
+        let a = A;
+        let _: E = a.clone();
+        let _: E = *****a;
+    }
+
+    fn check(mut encoded: &[u8]) {
+        let _ = &mut encoded.clone();
+        let _ = &encoded.clone();
+    }
 }