Skip to content

New lint: manual_memmove #8335

@untitaker

Description

@untitaker

What it does

Like manual_memcpy, find for-loops equivalent to slice::copy_within.

Lint Name

manual_memmove

Category

perf

Advantage

It's shorter and faster.

Drawbacks

any case where memmove is not actually faster? (should probably file as bug against rust though)

Example

let mut arr: [u8; 4] = [1, 2, 3, 4];
                                     
for i in 0..2 {
    arr[i] = arr[i + 2];
}

Could be written as:

arr.copy_within(2..4, 0);

Implementation notes

More generally:

for i in a..b {
    arr[i] = arr[i + offset];
}

Could be written as:

arr.copy_within((a + offset)..(b + offset), a);

Definetly need to look at implementation of manual_memmove and its tests to cover all the edgecases around false-positives here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions