Skip to content

Commit 61e2b7a

Browse files
authored
Merge pull request #2355 from alusch/clone_on_ref_ptr_restriction
Fix #2048: Move `clone_on_ref_ptr` to the restriction lints
2 parents 28c3d03 + f343cd2 commit 61e2b7a

File tree

3 files changed

+59
-46
lines changed

3 files changed

+59
-46
lines changed

clippy_lints/src/methods.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,8 @@ declare_lint! {
361361
/// ```rust
362362
/// x.clone()
363363
/// ```
364-
declare_lint! {
364+
declare_restriction_lint! {
365365
pub CLONE_ON_REF_PTR,
366-
Warn,
367366
"using 'clone' on a ref-counted pointer"
368367
}
369368

@@ -1013,24 +1012,26 @@ fn lint_clone_on_copy(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr, arg_t
10131012
fn lint_clone_on_ref_ptr(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr) {
10141013
let (obj_ty, _) = walk_ptrs_ty_depth(cx.tables.expr_ty(arg));
10151014

1016-
let caller_type = if match_type(cx, obj_ty, &paths::RC) {
1017-
"Rc"
1018-
} else if match_type(cx, obj_ty, &paths::ARC) {
1019-
"Arc"
1020-
} else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
1021-
"Weak"
1022-
} else {
1023-
return;
1024-
};
1015+
if let ty::TyAdt(_, subst) = obj_ty.sty {
1016+
let caller_type = if match_type(cx, obj_ty, &paths::RC) {
1017+
"Rc"
1018+
} else if match_type(cx, obj_ty, &paths::ARC) {
1019+
"Arc"
1020+
} else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
1021+
"Weak"
1022+
} else {
1023+
return;
1024+
};
10251025

1026-
span_lint_and_sugg(
1027-
cx,
1028-
CLONE_ON_REF_PTR,
1029-
expr.span,
1030-
"using '.clone()' on a ref-counted pointer",
1031-
"try this",
1032-
format!("{}::clone(&{})", caller_type, snippet(cx, arg.span, "_")),
1033-
);
1026+
span_lint_and_sugg(
1027+
cx,
1028+
CLONE_ON_REF_PTR,
1029+
expr.span,
1030+
"using '.clone()' on a ref-counted pointer",
1031+
"try this",
1032+
format!("{}::<{}>::clone(&{})", caller_type, subst.type_at(0), snippet(cx, arg.span, "_")),
1033+
);
1034+
}
10341035
}
10351036

10361037

tests/ui/unnecessary_clone.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
#![warn(clone_on_ref_ptr)]
12
#![allow(unused)]
23

34
use std::collections::HashSet;
45
use std::collections::VecDeque;
56
use std::rc::{self, Rc};
67
use std::sync::{self, Arc};
78

9+
trait SomeTrait {}
10+
struct SomeImpl;
11+
impl SomeTrait for SomeImpl {}
12+
813
fn main() {}
914

1015
fn clone_on_copy() {
@@ -34,7 +39,8 @@ fn clone_on_ref_ptr() {
3439
arc_weak.clone();
3540
sync::Weak::clone(&arc_weak);
3641

37-
42+
let x = Arc::new(SomeImpl);
43+
let _: Arc<SomeTrait> = x.clone();
3844
}
3945

4046
fn clone_on_copy_generic<T: Copy>(t: T) {

tests/ui/unnecessary_clone.stderr

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,81 @@
11
error: using `clone` on a `Copy` type
2-
--> $DIR/unnecessary_clone.rs:11:5
2+
--> $DIR/unnecessary_clone.rs:16:5
33
|
4-
11 | 42.clone();
4+
16 | 42.clone();
55
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
66
|
77
= note: `-D clone-on-copy` implied by `-D warnings`
88

99
error: using `clone` on a `Copy` type
10-
--> $DIR/unnecessary_clone.rs:15:5
10+
--> $DIR/unnecessary_clone.rs:20:5
1111
|
12-
15 | (&42).clone();
12+
20 | (&42).clone();
1313
| ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)`
1414

1515
error: using '.clone()' on a ref-counted pointer
16-
--> $DIR/unnecessary_clone.rs:25:5
16+
--> $DIR/unnecessary_clone.rs:30:5
1717
|
18-
25 | rc.clone();
19-
| ^^^^^^^^^^ help: try this: `Rc::clone(&rc)`
18+
30 | rc.clone();
19+
| ^^^^^^^^^^ help: try this: `Rc::<bool>::clone(&rc)`
2020
|
2121
= note: `-D clone-on-ref-ptr` implied by `-D warnings`
2222

2323
error: using '.clone()' on a ref-counted pointer
24-
--> $DIR/unnecessary_clone.rs:28:5
24+
--> $DIR/unnecessary_clone.rs:33:5
2525
|
26-
28 | arc.clone();
27-
| ^^^^^^^^^^^ help: try this: `Arc::clone(&arc)`
26+
33 | arc.clone();
27+
| ^^^^^^^^^^^ help: try this: `Arc::<bool>::clone(&arc)`
2828

2929
error: using '.clone()' on a ref-counted pointer
30-
--> $DIR/unnecessary_clone.rs:31:5
30+
--> $DIR/unnecessary_clone.rs:36:5
3131
|
32-
31 | rcweak.clone();
33-
| ^^^^^^^^^^^^^^ help: try this: `Weak::clone(&rcweak)`
32+
36 | rcweak.clone();
33+
| ^^^^^^^^^^^^^^ help: try this: `Weak::<bool>::clone(&rcweak)`
3434

3535
error: using '.clone()' on a ref-counted pointer
36-
--> $DIR/unnecessary_clone.rs:34:5
36+
--> $DIR/unnecessary_clone.rs:39:5
3737
|
38-
34 | arc_weak.clone();
39-
| ^^^^^^^^^^^^^^^^ help: try this: `Weak::clone(&arc_weak)`
38+
39 | arc_weak.clone();
39+
| ^^^^^^^^^^^^^^^^ help: try this: `Weak::<bool>::clone(&arc_weak)`
40+
41+
error: using '.clone()' on a ref-counted pointer
42+
--> $DIR/unnecessary_clone.rs:43:29
43+
|
44+
43 | let _: Arc<SomeTrait> = x.clone();
45+
| ^^^^^^^^^ help: try this: `Arc::<SomeImpl>::clone(&x)`
4046

4147
error: using `clone` on a `Copy` type
42-
--> $DIR/unnecessary_clone.rs:41:5
48+
--> $DIR/unnecessary_clone.rs:47:5
4349
|
44-
41 | t.clone();
50+
47 | t.clone();
4551
| ^^^^^^^^^ help: try removing the `clone` call: `t`
4652

4753
error: using `clone` on a `Copy` type
48-
--> $DIR/unnecessary_clone.rs:43:5
54+
--> $DIR/unnecessary_clone.rs:49:5
4955
|
50-
43 | Some(t).clone();
56+
49 | Some(t).clone();
5157
| ^^^^^^^^^^^^^^^ help: try removing the `clone` call: `Some(t)`
5258

5359
error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
54-
--> $DIR/unnecessary_clone.rs:49:22
60+
--> $DIR/unnecessary_clone.rs:55:22
5561
|
56-
49 | let z: &Vec<_> = y.clone();
62+
55 | let z: &Vec<_> = y.clone();
5763
| ^^^^^^^^^
5864
|
5965
= note: `-D clone-double-ref` implied by `-D warnings`
6066
help: try dereferencing it
6167
|
62-
49 | let z: &Vec<_> = &(*y).clone();
68+
55 | let z: &Vec<_> = &(*y).clone();
6369
| ^^^^^^^^^^^^^
6470
help: or try being explicit about what type to clone
6571
|
66-
49 | let z: &Vec<_> = &std::vec::Vec<i32>::clone(y);
72+
55 | let z: &Vec<_> = &std::vec::Vec<i32>::clone(y);
6773
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6874

6975
error: called `cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
70-
--> $DIR/unnecessary_clone.rs:56:27
76+
--> $DIR/unnecessary_clone.rs:62:27
7177
|
72-
56 | let v2 : Vec<isize> = v.iter().cloned().collect();
78+
62 | let v2 : Vec<isize> = v.iter().cloned().collect();
7379
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
7480
|
7581
= note: `-D iter-cloned-collect` implied by `-D warnings`

0 commit comments

Comments
 (0)