Skip to content

New lint: out_of_bound_slice_operation #14960

Open
@sosthene-nitrokey

Description

@sosthene-nitrokey

What it does

Lint against uses of copy_from_slice, clone_from_slice and swap_with_slice that are guaranteed to panic.

This could also be made part of out_of_bounds_indexing but there is precedent for having this be a separate, warn-by-default lint with iter_out_of_bounds.

Advantage

  • Remove code that is obviously erroneous

Drawbacks

No response

Example

The following would be linted (including the clone and swap versions) because it will always panic:

[1u8;10].copy_from_slice(&[1u8; 9]);
let mut a = [1u8;10];
let b = [1u8;9];
a.copy_from_slice(&b);

Ideally it would also lint cases where there is a simple slicing operation before the call:

let mut a = [1u8;10];
let b = [1u8;9];
a[..8].copy_from_slice(&b[..6]);

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