split unnecessary_clone test into clone_on_ref_ptr and clone_on_copy

This commit is contained in:
Ada Alakbarova 2025-09-23 14:34:38 +02:00
parent 6b71c164e3
commit 220e137089
No known key found for this signature in database
8 changed files with 261 additions and 211 deletions

View file

@ -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
@ -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)?
//

View file

@ -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
@ -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)?
//

View file

@ -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`
@ -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

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -0,0 +1,41 @@
error: using `.clone()` on a ref-counted pointer
--> tests/ui/clone_on_ref_ptr.rs:9:5
|
LL | rc.clone();
| ^^^^^^^^^^ help: try: `std::rc::Rc::<str>::clone(&rc)`
|
= note: `-D clippy::clone-on-ref-ptr` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::clone_on_ref_ptr)]`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/clone_on_ref_ptr.rs:11:5
|
LL | rc_weak.clone();
| ^^^^^^^^^^^^^^^ help: try: `std::rc::Weak::<str>::clone(&rc_weak)`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/clone_on_ref_ptr.rs:13:5
|
LL | arc.clone();
| ^^^^^^^^^^^ help: try: `std::sync::Arc::<str>::clone(&arc)`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/clone_on_ref_ptr.rs:15:5
|
LL | arc_weak.clone();
| ^^^^^^^^^^^^^^^^ help: try: `std::sync::Weak::<str>::clone(&arc_weak)`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/clone_on_ref_ptr.rs:30:33
|
LL | let _: Arc<dyn SomeTrait> = x.clone();
| ^^^^^^^^^ help: try: `std::sync::Arc::<SomeImpl>::clone(&x)`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/clone_on_ref_ptr.rs:48:14
|
LL | Some(try_opt!(Some(rc)).clone())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::rc::Rc::<u8>::clone(&try_opt!(Some(rc)))`
error: aborting due to 6 previous errors

View file

@ -1,111 +0,0 @@
// does not test any rustfixable lints
#![warn(clippy::clone_on_ref_ptr)]
#![allow(unused)]
#![allow(clippy::redundant_clone, clippy::uninlined_format_args, clippy::unnecessary_wraps)]
//@no-rustfix
use std::cell::RefCell;
use std::rc::{self, Rc};
use std::sync::{self, Arc};
trait SomeTrait {}
struct SomeImpl;
impl SomeTrait for SomeImpl {}
fn main() {}
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();
//~^ clone_on_ref_ptr
Rc::clone(&rc);
arc.clone();
//~^ clone_on_ref_ptr
Arc::clone(&arc);
rcweak.clone();
//~^ clone_on_ref_ptr
rc::Weak::clone(&rcweak);
arc_weak.clone();
//~^ clone_on_ref_ptr
sync::Weak::clone(&arc_weak);
let x = Arc::new(SomeImpl);
let _: Arc<dyn SomeTrait> = x.clone();
//~^ clone_on_ref_ptr
}
fn clone_on_copy_generic<T: Copy>(t: T) {
t.clone();
//~^ clone_on_copy
Some(t).clone();
//~^ clone_on_copy
}
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();
//~^ clone_on_copy
let _: E = *****a;
}
}
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
}
}

View file

@ -1,62 +0,0 @@
error: using `.clone()` on a ref-counted pointer
--> tests/ui/unnecessary_clone.rs:23:5
|
LL | rc.clone();
| ^^^^^^^^^^ help: try: `std::rc::Rc::<bool>::clone(&rc)`
|
= note: `-D clippy::clone-on-ref-ptr` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::clone_on_ref_ptr)]`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/unnecessary_clone.rs:28:5
|
LL | arc.clone();
| ^^^^^^^^^^^ help: try: `std::sync::Arc::<bool>::clone(&arc)`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/unnecessary_clone.rs:33:5
|
LL | rcweak.clone();
| ^^^^^^^^^^^^^^ help: try: `std::rc::Weak::<bool>::clone(&rcweak)`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/unnecessary_clone.rs:38:5
|
LL | arc_weak.clone();
| ^^^^^^^^^^^^^^^^ help: try: `std::sync::Weak::<bool>::clone(&arc_weak)`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/unnecessary_clone.rs:44:33
|
LL | let _: Arc<dyn SomeTrait> = x.clone();
| ^^^^^^^^^ help: try: `std::sync::Arc::<SomeImpl>::clone(&x)`
error: using `clone` on type `T` which implements the `Copy` trait
--> tests/ui/unnecessary_clone.rs:49:5
|
LL | t.clone();
| ^^^^^^^^^ help: try removing the `clone` call: `t`
|
= note: `-D clippy::clone-on-copy` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::clone_on_copy)]`
error: using `clone` on type `Option<T>` which implements the `Copy` trait
--> tests/ui/unnecessary_clone.rs:52:5
|
LL | Some(t).clone();
| ^^^^^^^^^^^^^^^ help: try removing the `clone` call: `Some(t)`
error: using `clone` on type `E` which implements the `Copy` trait
--> tests/ui/unnecessary_clone.rs:87:20
|
LL | let _: E = a.clone();
| ^^^^^^^^^ help: try dereferencing it: `*****a`
error: using `.clone()` on a ref-counted pointer
--> tests/ui/unnecessary_clone.rs:108:14
|
LL | Some(try_opt!(Some(rc)).clone())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::rc::Rc::<u8>::clone(&try_opt!(Some(rc)))`
error: aborting due to 9 previous errors