Skip to content

Commit a5cdd4a

Browse files
committed
Ignore not really redundant clones of ManuallyDrop
"Redundant" clones of `ManuallyDrop` are sometimes used for the side effect of invoking the clone, without running the drop implementation of the inner type. In other words, they aren't really redundant. For example, futures-rs crate: ```rust #[allow(clippy::redundant_clone)] // The clone here isn't actually redundant. unsafe fn increase_refcount<T: ArcWake>(data: *const ()) { // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(data as *const T)); // Now increase refcount, but don't drop new refcount either let _arc_clone: mem::ManuallyDrop<_> = arc.clone(); } ``` Ignore redundant clone lint for ManuallyDrop.
1 parent 8cf4219 commit a5cdd4a

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

clippy_lints/src/redundant_clone.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
124124
continue;
125125
}
126126

127+
if let ty::Adt(ref def, _) = arg_ty.kind {
128+
if match_def_path(cx, def.did, &paths::MEM_MANUALLY_DROP) {
129+
continue;
130+
}
131+
}
132+
127133
// `{ cloned = &arg; clone(move cloned); }` or `{ cloned = &arg; to_path_buf(cloned); }`
128134
let (cloned, cannot_move_out) = unwrap_or_continue!(find_stmt_assigns_to(cx, mir, arg, from_borrow, bb));
129135

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "Link
5959
pub const LINT: [&str; 3] = ["rustc_session", "lint", "Lint"];
6060
pub const MEM_DISCRIMINANT: [&str; 3] = ["core", "mem", "discriminant"];
6161
pub const MEM_FORGET: [&str; 3] = ["core", "mem", "forget"];
62+
pub const MEM_MANUALLY_DROP: [&str; 4] = ["core", "mem", "manually_drop", "ManuallyDrop"];
6263
pub const MEM_MAYBEUNINIT: [&str; 4] = ["core", "mem", "maybe_uninit", "MaybeUninit"];
6364
pub const MEM_MAYBEUNINIT_UNINIT: [&str; 5] = ["core", "mem", "maybe_uninit", "MaybeUninit", "uninit"];
6465
pub const MEM_REPLACE: [&str; 3] = ["core", "mem", "replace"];

tests/ui/redundant_clone.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fn main() {
5252
borrower_propagation();
5353
not_consumed();
5454
issue_5405();
55+
manually_drop();
5556
}
5657

5758
#[derive(Clone)]
@@ -170,3 +171,17 @@ fn issue_5405() {
170171
let c: [usize; 2] = [2, 3];
171172
let _d: usize = c[1].clone();
172173
}
174+
175+
fn manually_drop() {
176+
use std::mem::ManuallyDrop;
177+
use std::sync::Arc;
178+
179+
let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
180+
let _ = a.clone(); // OK
181+
182+
let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));
183+
unsafe {
184+
Arc::from_raw(p);
185+
Arc::from_raw(p);
186+
}
187+
}

tests/ui/redundant_clone.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fn main() {
5252
borrower_propagation();
5353
not_consumed();
5454
issue_5405();
55+
manually_drop();
5556
}
5657

5758
#[derive(Clone)]
@@ -170,3 +171,17 @@ fn issue_5405() {
170171
let c: [usize; 2] = [2, 3];
171172
let _d: usize = c[1].clone();
172173
}
174+
175+
fn manually_drop() {
176+
use std::mem::ManuallyDrop;
177+
use std::sync::Arc;
178+
179+
let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
180+
let _ = a.clone(); // OK
181+
182+
let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));
183+
unsafe {
184+
Arc::from_raw(p);
185+
Arc::from_raw(p);
186+
}
187+
}

tests/ui/redundant_clone.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,61 +108,61 @@ LL | let _t = tup.0.clone();
108108
| ^^^^^
109109

110110
error: redundant clone
111-
--> $DIR/redundant_clone.rs:61:22
111+
--> $DIR/redundant_clone.rs:62:22
112112
|
113113
LL | (a.clone(), a.clone())
114114
| ^^^^^^^^ help: remove this
115115
|
116116
note: this value is dropped without further use
117-
--> $DIR/redundant_clone.rs:61:21
117+
--> $DIR/redundant_clone.rs:62:21
118118
|
119119
LL | (a.clone(), a.clone())
120120
| ^
121121

122122
error: redundant clone
123-
--> $DIR/redundant_clone.rs:121:15
123+
--> $DIR/redundant_clone.rs:122:15
124124
|
125125
LL | let _s = s.clone();
126126
| ^^^^^^^^ help: remove this
127127
|
128128
note: this value is dropped without further use
129-
--> $DIR/redundant_clone.rs:121:14
129+
--> $DIR/redundant_clone.rs:122:14
130130
|
131131
LL | let _s = s.clone();
132132
| ^
133133

134134
error: redundant clone
135-
--> $DIR/redundant_clone.rs:122:15
135+
--> $DIR/redundant_clone.rs:123:15
136136
|
137137
LL | let _t = t.clone();
138138
| ^^^^^^^^ help: remove this
139139
|
140140
note: this value is dropped without further use
141-
--> $DIR/redundant_clone.rs:122:14
141+
--> $DIR/redundant_clone.rs:123:14
142142
|
143143
LL | let _t = t.clone();
144144
| ^
145145

146146
error: redundant clone
147-
--> $DIR/redundant_clone.rs:132:19
147+
--> $DIR/redundant_clone.rs:133:19
148148
|
149149
LL | let _f = f.clone();
150150
| ^^^^^^^^ help: remove this
151151
|
152152
note: this value is dropped without further use
153-
--> $DIR/redundant_clone.rs:132:18
153+
--> $DIR/redundant_clone.rs:133:18
154154
|
155155
LL | let _f = f.clone();
156156
| ^
157157

158158
error: redundant clone
159-
--> $DIR/redundant_clone.rs:144:14
159+
--> $DIR/redundant_clone.rs:145:14
160160
|
161161
LL | let y = x.clone().join("matthias");
162162
| ^^^^^^^^ help: remove this
163163
|
164164
note: cloned value is neither consumed nor mutated
165-
--> $DIR/redundant_clone.rs:144:13
165+
--> $DIR/redundant_clone.rs:145:13
166166
|
167167
LL | let y = x.clone().join("matthias");
168168
| ^^^^^^^^^

0 commit comments

Comments
 (0)