Skip to content

Commit 52b2a5e

Browse files
committed
Auto merge of #12529 - samueltardieu:issue-12528, r=y21
Do not warn on .map(_::clone) for Arc, Rc, and their weak variants Those constructions are idiomatic, and using `Arc::clone(x)` and `Rc::clone(x)` is often the recommended way of cloning a `Arc` or a `Rc`. Fix #12528 changelog: [`map_clone`]: do not warn on `.map(_::clone)` for `Arc`, `Rc`, and their `Weak` variants
2 parents b5e7394 + 7c9fe30 commit 52b2a5e

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

clippy_lints/src/methods/map_clone.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ fn handle_path(
124124
&& let ty::Ref(_, ty, Mutability::Not) = ty.kind()
125125
&& let ty::FnDef(_, lst) = cx.typeck_results().expr_ty(arg).kind()
126126
&& lst.iter().all(|l| l.as_type() == Some(*ty))
127+
&& !matches!(
128+
ty.ty_adt_def()
129+
.and_then(|adt_def| cx.tcx.get_diagnostic_name(adt_def.did())),
130+
Some(sym::Arc | sym::ArcWeak | sym::Rc | sym::RcWeak)
131+
)
127132
{
128133
lint_path(cx, e.span, recv.span, is_copy(cx, ty.peel_refs()));
129134
}

tests/ui/map_clone.fixed

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,28 @@ fn main() {
131131
let x: Vec<&u8> = vec![];
132132
let y = x.into_iter().map(|x| u8::clone(loop {}));
133133
}
134+
135+
// Issue #12528
136+
{
137+
// Don't lint these
138+
use std::rc::{Rc, Weak as RcWeak};
139+
use std::sync::{Arc, Weak as ArcWeak};
140+
struct Foo;
141+
142+
let x = Arc::new(Foo);
143+
let y = Some(&x);
144+
let _z = y.map(Arc::clone);
145+
146+
let x = Rc::new(Foo);
147+
let y = Some(&x);
148+
let _z = y.map(Rc::clone);
149+
150+
let x = Arc::downgrade(&Arc::new(Foo));
151+
let y = Some(&x);
152+
let _z = y.map(ArcWeak::clone);
153+
154+
let x = Rc::downgrade(&Rc::new(Foo));
155+
let y = Some(&x);
156+
let _z = y.map(RcWeak::clone);
157+
}
134158
}

tests/ui/map_clone.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,28 @@ fn main() {
131131
let x: Vec<&u8> = vec![];
132132
let y = x.into_iter().map(|x| u8::clone(loop {}));
133133
}
134+
135+
// Issue #12528
136+
{
137+
// Don't lint these
138+
use std::rc::{Rc, Weak as RcWeak};
139+
use std::sync::{Arc, Weak as ArcWeak};
140+
struct Foo;
141+
142+
let x = Arc::new(Foo);
143+
let y = Some(&x);
144+
let _z = y.map(Arc::clone);
145+
146+
let x = Rc::new(Foo);
147+
let y = Some(&x);
148+
let _z = y.map(Rc::clone);
149+
150+
let x = Arc::downgrade(&Arc::new(Foo));
151+
let y = Some(&x);
152+
let _z = y.map(ArcWeak::clone);
153+
154+
let x = Rc::downgrade(&Rc::new(Foo));
155+
let y = Some(&x);
156+
let _z = y.map(RcWeak::clone);
157+
}
134158
}

0 commit comments

Comments
 (0)