Skip to content

Commit

Permalink
Deny clippy::clone_on_ref_ptr
Browse files Browse the repository at this point in the history
**This Commit**
Denies the use of `.clone()` on reference counted pointers as advised by
[this `clippy` lint][0] and [The Rust Book][1]. It also addresses
instances where the lint was violated.

**Why?**
It's best practice to make clear the clone is cheap. There is [some
debate][2] on the issue and the [standard library no longer explicitly
recommends it][3] but it is still noted as more idiomatic.

In any case, for us there are no issues making the change and if there
is a possibility for being more expressive, we should take it.

See #6 (comment)
for more details.

[0]: https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_ref_ptr
[1]: https://doc.rust-lang.org/book/ch15-04-rc.html#using-rct-to-share-data
[2]: rust-lang/rust-clippy#2048
[3]: rust-lang/rust#76138
  • Loading branch information
mlodato517 committed Nov 24, 2021
1 parent 1cba1fe commit 18f0d44
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
52 changes: 28 additions & 24 deletions src/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,21 @@ impl<K, V> Tree<K, V> {
Tree::Node(n) => match key.cmp(&n.key) {
cmp::Ordering::Less => Tree::Node(Node {
left: Rc::new(n.left.insert(key, value)),
key: n.key.clone(),
right: n.right.clone(),
value: n.value.clone(),
key: Rc::clone(&n.key),
right: Rc::clone(&n.right),
value: Rc::clone(&n.value),
}),
cmp::Ordering::Equal => Tree::Node(Node {
value: Rc::new(value),
key: n.key.clone(),
left: n.left.clone(),
right: n.right.clone(),
key: Rc::clone(&n.key),
left: Rc::clone(&n.left),
right: Rc::clone(&n.right),
}),
cmp::Ordering::Greater => Tree::Node(Node {
right: Rc::new(n.right.insert(key, value)),
key: n.key.clone(),
left: n.left.clone(),
value: n.value.clone(),
key: Rc::clone(&n.key),
left: Rc::clone(&n.left),
value: Rc::clone(&n.value),
}),
},
}
Expand Down Expand Up @@ -162,9 +162,9 @@ impl<K, V> Tree<K, V> {
Tree::Node(n) => match k.cmp(&n.key) {
cmp::Ordering::Less => Tree::Node(Node {
left: Rc::new(n.left.delete(k)),
key: n.key.clone(),
right: n.right.clone(),
value: n.value.clone(),
key: Rc::clone(&n.key),
right: Rc::clone(&n.right),
value: Rc::clone(&n.value),
}),
cmp::Ordering::Equal => match (n.left.as_ref(), n.right.as_ref()) {
(Tree::Leaf, Tree::Leaf) => Tree::Leaf,
Expand All @@ -179,17 +179,17 @@ impl<K, V> Tree<K, V> {
let (pred_key, pred_val, new_left) = left_child.delete_largest();
Tree::Node(Node {
left: new_left,
right: n.right.clone(),
right: Rc::clone(&n.right),
key: pred_key,
value: pred_val,
})
}
},
cmp::Ordering::Greater => Tree::Node(Node {
right: Rc::new(n.right.delete(k)),
key: n.key.clone(),
left: n.left.clone(),
value: n.value.clone(),
key: Rc::clone(&n.key),
left: Rc::clone(&n.left),
value: Rc::clone(&n.value),
}),
},
}
Expand All @@ -214,10 +214,10 @@ pub struct Node<K, V> {
impl<K, V> Clone for Node<K, V> {
fn clone(&self) -> Self {
Self {
key: self.key.clone(),
value: self.value.clone(),
left: self.left.clone(),
right: self.right.clone(),
key: Rc::clone(&self.key),
left: Rc::clone(&self.left),
right: Rc::clone(&self.right),
value: Rc::clone(&self.value),
}
}
}
Expand All @@ -239,7 +239,11 @@ impl<K, V> Node<K, V> {
K: cmp::Ord,
{
match self.right.as_ref() {
Tree::Leaf => (self.key.clone(), self.value.clone(), self.left.clone()),
Tree::Leaf => (
Rc::clone(&self.key),
Rc::clone(&self.value),
Rc::clone(&self.left),
),
Tree::Node(r) => {
let (key, value, sub) = r.delete_largest();

Expand All @@ -248,9 +252,9 @@ impl<K, V> Node<K, V> {
value,
Rc::new(Tree::Node(Node {
right: sub,
left: self.left.clone(),
key: self.key.clone(),
value: self.value.clone(),
key: Rc::clone(&self.key),
left: Rc::clone(&self.left),
value: Rc::clone(&self.value),
})),
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
//! in the tree. BSTs also naturally support sorted iteration by visiting the
//! left subtree, then the subtree root, then the right subtree.

#![deny(missing_docs)]
#![deny(missing_docs, clippy::clone_on_ref_ptr)]

pub mod functional;

0 comments on commit 18f0d44

Please sign in to comment.