Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions tests/ui/clone_on_copy.fixed
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
#![warn(clippy::clone_on_copy)]
#![allow(
unused,
clippy::redundant_clone,
clippy::deref_addrof,
clippy::no_effect,
clippy::unnecessary_operation,
clippy::vec_init_then_push,
clippy::toplevel_ref_arg,
clippy::needless_borrow
)]

use std::cell::RefCell;
use std::rc::{self, Rc};
use std::sync::{self, Arc};

fn main() {}

fn is_ascii(ch: char) -> bool {
ch.is_ascii()
}

fn clone_on_copy() -> Option<(i32)> {
fn main() {
42;
//~^ clone_on_copy

Expand Down Expand Up @@ -65,20 +56,66 @@ fn clone_on_copy() -> Option<(i32)> {
let x = 42;
let ref y = x.clone(); // ok, binds by reference
let ref mut y = x.clone(); // ok, binds by reference
}

mod issue3052 {
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_on_copy

let _: E = *****a;
}
}

fn issue4348() {
fn is_ascii(ch: char) -> bool {
ch.is_ascii()
}

// 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_on_copy
}

// Issue #5436
#[expect(clippy::vec_init_then_push)]
fn issue5436() {
let mut vec = Vec::new();
vec.push(42);
//~^ clone_on_copy
}

// Issue #9277
fn issue9277() -> Option<i32> {
let opt: &Option<i32> = &None;
let value = (*opt)?; // operator precedence needed (*opt)?
//
Expand Down
65 changes: 51 additions & 14 deletions tests/ui/clone_on_copy.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
#![warn(clippy::clone_on_copy)]
#![allow(
unused,
clippy::redundant_clone,
clippy::deref_addrof,
clippy::no_effect,
clippy::unnecessary_operation,
clippy::vec_init_then_push,
clippy::toplevel_ref_arg,
clippy::needless_borrow
)]

use std::cell::RefCell;
use std::rc::{self, Rc};
use std::sync::{self, Arc};

fn main() {}

fn is_ascii(ch: char) -> bool {
ch.is_ascii()
}

fn clone_on_copy() -> Option<(i32)> {
fn main() {
42.clone();
//~^ clone_on_copy

Expand Down Expand Up @@ -65,20 +56,66 @@ fn clone_on_copy() -> Option<(i32)> {
let x = 42;
let ref y = x.clone(); // ok, binds by reference
let ref mut y = x.clone(); // ok, binds by reference
}

mod issue3052 {
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();
//~^ clone_on_copy

let _: E = *****a;
}
}

fn issue4348() {
fn is_ascii(ch: char) -> bool {
ch.is_ascii()
}

// 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());
//~^ clone_on_copy
}

// Issue #5436
#[expect(clippy::vec_init_then_push)]
fn issue5436() {
let mut vec = Vec::new();
vec.push(42.clone());
//~^ clone_on_copy
}

// Issue #9277
fn issue9277() -> Option<i32> {
let opt: &Option<i32> = &None;
let value = opt.clone()?; // operator precedence needed (*opt)?
//
Expand Down
26 changes: 16 additions & 10 deletions tests/ui/clone_on_copy.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: using `clone` on type `i32` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:23:5
--> tests/ui/clone_on_copy.rs:14:5
|
LL | 42.clone();
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
Expand All @@ -8,52 +8,58 @@ LL | 42.clone();
= help: to override `-D warnings` add `#[allow(clippy::clone_on_copy)]`

error: using `clone` on type `i32` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:28:5
--> tests/ui/clone_on_copy.rs:19:5
|
LL | (&42).clone();
| ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)`

error: using `clone` on type `i32` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:32:5
--> tests/ui/clone_on_copy.rs:23:5
|
LL | rc.borrow().clone();
| ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*rc.borrow()`

error: using `clone` on type `u32` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:36:5
--> tests/ui/clone_on_copy.rs:27:5
|
LL | x.clone().rotate_left(1);
| ^^^^^^^^^ help: try removing the `clone` call: `x`

error: using `clone` on type `i32` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:51:5
--> tests/ui/clone_on_copy.rs:42:5
|
LL | m!(42).clone();
| ^^^^^^^^^^^^^^ help: try removing the `clone` call: `m!(42)`

error: using `clone` on type `[u32; 2]` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:62:5
--> tests/ui/clone_on_copy.rs:53:5
|
LL | x.clone()[0];
| ^^^^^^^^^ help: try dereferencing it: `(*x)`

error: using `clone` on type `E` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:92:20
|
LL | let _: E = a.clone();
| ^^^^^^^^^ help: try dereferencing it: `*****a`

error: using `clone` on type `char` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:73:14
--> tests/ui/clone_on_copy.rs:107:14
|
LL | is_ascii('z'.clone());
| ^^^^^^^^^^^ help: try removing the `clone` call: `'z'`

error: using `clone` on type `i32` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:78:14
--> tests/ui/clone_on_copy.rs:114:14
|
LL | vec.push(42.clone());
| ^^^^^^^^^^ help: try removing the `clone` call: `42`

error: using `clone` on type `Option<i32>` which implements the `Copy` trait
--> tests/ui/clone_on_copy.rs:83:17
--> tests/ui/clone_on_copy.rs:120:17
|
LL | let value = opt.clone()?; // operator precedence needed (*opt)?
| ^^^^^^^^^^^ help: try dereferencing it: `(*opt)`

error: aborting due to 9 previous errors
error: aborting due to 10 previous errors

51 changes: 51 additions & 0 deletions tests/ui/clone_on_ref_ptr.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![warn(clippy::clone_on_ref_ptr)]

use std::rc::{Rc, Weak as RcWeak};
use std::sync::{Arc, Weak as ArcWeak};

fn main() {}

fn clone_on_ref_ptr(rc: Rc<str>, rc_weak: RcWeak<str>, arc: Arc<str>, arc_weak: ArcWeak<str>) {
std::rc::Rc::<str>::clone(&rc);
//~^ clone_on_ref_ptr
std::rc::Weak::<str>::clone(&rc_weak);
//~^ clone_on_ref_ptr
std::sync::Arc::<str>::clone(&arc);
//~^ clone_on_ref_ptr
std::sync::Weak::<str>::clone(&arc_weak);
//~^ clone_on_ref_ptr

Rc::clone(&rc);
Arc::clone(&arc);
RcWeak::clone(&rc_weak);
ArcWeak::clone(&arc_weak);
}

trait SomeTrait {}
struct SomeImpl;
impl SomeTrait for SomeImpl {}

fn trait_object() {
let x = Arc::new(SomeImpl);
let _: Arc<dyn SomeTrait> = std::sync::Arc::<SomeImpl>::clone(&x);
//~^ clone_on_ref_ptr
}

mod issue2076 {
use std::rc::Rc;

macro_rules! try_opt {
($expr: expr) => {
match $expr {
Some(value) => value,
None => return None,
}
};
}

fn func() -> Option<Rc<u8>> {
let rc = Rc::new(42);
Some(std::rc::Rc::<u8>::clone(&try_opt!(Some(rc))))
//~^ clone_on_ref_ptr
}
}
51 changes: 51 additions & 0 deletions tests/ui/clone_on_ref_ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![warn(clippy::clone_on_ref_ptr)]

use std::rc::{Rc, Weak as RcWeak};
use std::sync::{Arc, Weak as ArcWeak};

fn main() {}

fn clone_on_ref_ptr(rc: Rc<str>, rc_weak: RcWeak<str>, arc: Arc<str>, arc_weak: ArcWeak<str>) {
rc.clone();
//~^ clone_on_ref_ptr
rc_weak.clone();
//~^ clone_on_ref_ptr
arc.clone();
//~^ clone_on_ref_ptr
arc_weak.clone();
//~^ clone_on_ref_ptr

Rc::clone(&rc);
Arc::clone(&arc);
RcWeak::clone(&rc_weak);
ArcWeak::clone(&arc_weak);
}

trait SomeTrait {}
struct SomeImpl;
impl SomeTrait for SomeImpl {}

fn trait_object() {
let x = Arc::new(SomeImpl);
let _: Arc<dyn SomeTrait> = x.clone();
//~^ clone_on_ref_ptr
}

mod issue2076 {
use std::rc::Rc;

macro_rules! try_opt {
($expr: expr) => {
match $expr {
Some(value) => value,
None => return None,
}
};
}

fn func() -> Option<Rc<u8>> {
let rc = Rc::new(42);
Some(try_opt!(Some(rc)).clone())
//~^ clone_on_ref_ptr
}
}
Loading
Loading