Element-by-element copy loop between two slices
ID |
rust.performance.manual_memcpy |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Rust |
Description
Reports a for loop over an index range whose entire body copies one element from a source slice into a destination slice at the same index.
Rationale
Each pass bounds-checks both slices and moves one element, where the standard library copies the whole run in a single memmove. The hand-written form is also easy to get subtly wrong: the range bound, the source and the destination are three independent expressions, so a later edit can leave them describing different lengths and the loop will happily panic or copy a partial buffer.
The following code illustrates the pattern detected by this rule:
pub fn fill(dst: &mut [u8], src: &[u8]) {
// FLAGGED: Element-by-element copy loop between two slices
for i in 0..src.len() {
dst[i] = src[i];
}
}