2019-09-25 12:46:14 -05:00
|
|
|
// does not test any rustfixable lints
|
|
|
|
|
2018-07-28 10:34:52 -05:00
|
|
|
#![warn(clippy::clone_on_ref_ptr)]
|
2019-09-16 10:50:36 -05:00
|
|
|
#![allow(unused, clippy::redundant_clone)]
|
2017-10-09 23:07:12 -05:00
|
|
|
|
2018-10-18 23:03:56 -05:00
|
|
|
use std::cell::RefCell;
|
2017-10-09 23:07:12 -05:00
|
|
|
use std::rc::{self, Rc};
|
|
|
|
use std::sync::{self, Arc};
|
|
|
|
|
2018-01-14 21:58:09 -06:00
|
|
|
trait SomeTrait {}
|
|
|
|
struct SomeImpl;
|
|
|
|
impl SomeTrait for SomeImpl {}
|
|
|
|
|
2017-10-09 23:07:12 -05:00
|
|
|
fn main() {}
|
|
|
|
|
2020-04-09 08:50:09 -05:00
|
|
|
fn is_ascii(ch: char) -> bool {
|
|
|
|
ch.is_ascii()
|
|
|
|
}
|
|
|
|
|
2017-10-09 23:07:12 -05:00
|
|
|
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();
|
2018-10-18 23:03:56 -05:00
|
|
|
|
|
|
|
let rc = RefCell::new(0);
|
|
|
|
rc.borrow().clone();
|
2019-08-19 01:17:53 -05:00
|
|
|
|
|
|
|
// Issue #4348
|
|
|
|
let mut x = 43;
|
|
|
|
let _ = &x.clone(); // ok, getting a ref
|
|
|
|
'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
|
2020-04-09 08:50:09 -05:00
|
|
|
is_ascii('z'.clone());
|
|
|
|
|
|
|
|
// Issue #5436
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
vec.push(42.clone());
|
2017-10-09 23:07:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn clone_on_ref_ptr() {
|
|
|
|
let rc = Rc::new(true);
|
|
|
|
let arc = Arc::new(true);
|
|
|
|
|
|
|
|
let rcweak = Rc::downgrade(&rc);
|
|
|
|
let arc_weak = Arc::downgrade(&arc);
|
|
|
|
|
|
|
|
rc.clone();
|
|
|
|
Rc::clone(&rc);
|
|
|
|
|
|
|
|
arc.clone();
|
|
|
|
Arc::clone(&arc);
|
|
|
|
|
|
|
|
rcweak.clone();
|
|
|
|
rc::Weak::clone(&rcweak);
|
|
|
|
|
|
|
|
arc_weak.clone();
|
|
|
|
sync::Weak::clone(&arc_weak);
|
|
|
|
|
2018-01-14 21:58:09 -06:00
|
|
|
let x = Arc::new(SomeImpl);
|
2019-05-30 01:23:47 -05:00
|
|
|
let _: Arc<dyn SomeTrait> = x.clone();
|
2017-10-09 23:07:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn clone_on_copy_generic<T: Copy>(t: T) {
|
|
|
|
t.clone();
|
|
|
|
|
|
|
|
Some(t).clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clone_on_double_ref() {
|
|
|
|
let x = vec![1];
|
|
|
|
let y = &&x;
|
|
|
|
let z: &Vec<_> = y.clone();
|
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
println!("{:p} {:p}", *y, z);
|
2017-10-09 23:07:12 -05:00
|
|
|
}
|
|
|
|
|
2018-10-19 13:51:25 -05:00
|
|
|
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;
|
2018-12-09 16:26:16 -06:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&$dst
|
|
|
|
}
|
2018-10-19 13:51:25 -05:00
|
|
|
}
|
2018-12-09 16:26:16 -06:00
|
|
|
};
|
2018-10-19 13:51:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_deref!(A, B);
|
|
|
|
impl_deref!(B, C);
|
|
|
|
impl_deref!(C, D);
|
|
|
|
impl std::ops::Deref for D {
|
|
|
|
type Target = &'static E;
|
2018-12-09 16:26:16 -06:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&&E
|
|
|
|
}
|
2018-10-19 13:51:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn go1() {
|
|
|
|
let a = A;
|
|
|
|
let _: E = a.clone();
|
|
|
|
let _: E = *****a;
|
|
|
|
}
|
|
|
|
}
|