clone() called on a value of a Copy type
ID |
rust.maintainability.clone_on_copy |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundancy |
Language |
Rust |
Description
Reports clone() applied to a value of a primitive Copy type - an integer, float, bool or char - where assigning or passing the value already copies it.
Rationale
For a Copy type the call produces the same bit copy the compiler would have made anyway, so it adds nothing but noise. Worse, it reads as a deliberate deep copy and so signals that the value is expensive or shared when it is neither, and it hides the cases where a clone() in the same function really does matter. It is usually a leftover from code that once handled an owned heap type, or from silencing a borrow-checker error that has since been fixed.
The following code illustrates the pattern detected by this rule:
use std::collections::HashMap;
fn retries(configured: u32) -> u32 {
// FLAGGED: clone() called on a value of a Copy type
let budget = configured.clone();
budget
}