rust/tests/ui/unnecessary_clone.rs

115 lines
2.4 KiB
Rust
Raw Normal View History

2018-10-06 16:18:06 +00:00
// 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.
2018-07-28 15:34:52 +00:00
#![warn(clippy::clone_on_ref_ptr)]
2017-10-10 04:07:12 +00:00
#![allow(unused)]
use std::cell::RefCell;
2017-10-10 04:07:12 +00:00
use std::collections::HashSet;
use std::collections::VecDeque;
use std::rc::{self, Rc};
use std::sync::{self, Arc};
trait SomeTrait {}
struct SomeImpl;
impl SomeTrait for SomeImpl {}
2017-10-10 04:07:12 +00:00
fn main() {}
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();
2017-10-10 04:07:12 +00: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);
let x = Arc::new(SomeImpl);
2018-07-28 15:34:52 +00:00
let _: Arc<SomeTrait> = x.clone();
2017-10-10 04:07:12 +00: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 22:26:16 +00:00
println!("{:p} {:p}", *y, z);
2017-10-10 04:07:12 +00:00
}
fn iter_clone_collect() {
2018-12-09 22:26:16 +00:00
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();
2017-10-10 04:07:12 +00:00
}
2018-10-19 18:51:25 +00: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 22:26:16 +00:00
fn deref(&self) -> &Self::Target {
&$dst
}
2018-10-19 18:51:25 +00:00
}
2018-12-09 22:26:16 +00:00
};
2018-10-19 18:51:25 +00: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 22:26:16 +00:00
fn deref(&self) -> &Self::Target {
&&E
}
2018-10-19 18:51:25 +00:00
}
fn go1() {
let a = A;
let _: E = a.clone();
let _: E = *****a;
}
}