Skip to content
Open
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
34 changes: 21 additions & 13 deletions clippy_lints/src/methods/clone_on_ref_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::source::snippet_with_context;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_middle::ty::{self, IsSuggestable};
use rustc_span::symbol::{Symbol, sym};

use super::CLONE_ON_REF_PTR;
Expand All @@ -15,12 +15,10 @@ pub(super) fn check(
receiver: &hir::Expr<'_>,
args: &[hir::Expr<'_>],
) {
if !(args.is_empty() && method_name == sym::clone) {
return;
}
let obj_ty = cx.typeck_results().expr_ty(receiver).peel_refs();

if let ty::Adt(adt, subst) = obj_ty.kind()
if method_name == sym::clone
&& args.is_empty()
&& let obj_ty = cx.typeck_results().expr_ty(receiver).peel_refs()
&& let ty::Adt(adt, subst) = obj_ty.kind()
&& let Some(name) = cx.tcx.get_diagnostic_name(adt.did())
{
let caller_type = match name {
Expand All @@ -39,12 +37,22 @@ pub(super) fn check(
// Sometimes unnecessary ::<_> after Rc/Arc/Weak
let mut app = Applicability::Unspecified;
let snippet = snippet_with_context(cx, receiver.span, expr.span.ctxt(), "..", &mut app).0;
diag.span_suggestion(
expr.span,
"try",
format!("{caller_type}::<{}>::clone(&{snippet})", subst.type_at(0)),
app,
);
let generic = subst.type_at(0);
if generic.is_suggestable(cx.tcx, true) {
diag.span_suggestion(
expr.span,
"try",
format!("{caller_type}::<{generic}>::clone(&{snippet})"),
app,
);
} else {
diag.span_suggestion(
expr.span,
"try",
format!("{caller_type}::</* generic */>::clone(&{snippet})"),
Applicability::HasPlaceholders,
);
}
},
);
}
Expand Down
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

Loading