Closed
Description
Lint name:
#[warn(clippy::branches_sharing_code)]
I tried this code:
pub unsafe fn insert<K, V>(
root: Option<NonNull<Node<K, V>>>,
key: K,
val: V,
) -> Result<NonNull<Node<K, V>>, ()>
where
K: Ord,
{
let mut nq = NodeQuery::new(root);
let mut parent = None;
while nq.is_some() {
parent = nq.node;
match key.cmp(nq.get_key().unwrap()) {
Ordering::Less => nq = nq.left(),
Ordering::Greater => nq = nq.right(),
Ordering::Equal => {
nq.set_entry((key, Some(val))); // update val
return Err(());
}
}
}
//插入x
let mut x;
if let Some(mut node) = parent {
if key < node.as_ref().key {
x = Node::new_entry(key, val);
node.as_mut().left = Some(x);
} else {
x = Node::new_entry(key, val);
node.as_mut().right = Some(x);
}
x.as_mut().parent = parent;
} else {
x = Node::new_entry(key, val);
}
Ok(x)
}
I expected to see this happen: key is not Copy, so should not report the warning.
Instead, this happened: key is not Copy, clippy advice extract common codes
warning: all if blocks contain the same code at the start
--> src/tree/binary/bst.rs:92:9
|
92 | / if key < node.as_ref().key {
93 | | x = Node::new_entry(key, val);
| |__________________________________________^
|
= note: `#[warn(clippy::branches_sharing_code)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
help: consider moving the start statements out like this
|
92 | x = Node::new_entry(key, val);
93 | if key < node.as_ref().key {
Meta
cargo clippy -V
: clippy 0.1.53 (53cb7b09 2021-06-17)rustc -Vv
:
rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-apple-darwin
release: 1.53.0
LLVM version: 12.0.1