Skip to content

lint slicing of entire slices #6519

Closed
@matthiaskrgr

Description

@matthiaskrgr

What it does

In code like this

fn a(v: &Vec<String>) {
    let _get_me_a_slice: &[String] = &v[..];
}

clippy will suggest passing v as slice instead of Vec, resulting in this refactoring:

fn a(v: &[String]) {
    let _get_me_a_slice: &[String] = &v[..];
}

But this means we are slicing the slice to get a slice, we can remove the [..] now!

fn a(v: &[String]) {
    let _get_me_a_slice: &[String] = v;
}

It would be nice if clippy could warn about slicing of slices.

Categories (optional)

Kind: complexity?

What is the advantage of the recommended code over the original code

Simple code

Drawbacks

None.

Example

fn a(v: &[String]) {
    let _slice: &[String] = &v[..];
}

Could be written as:

fn a(v: &[String]) {
    let _slice: &[String] = v;
}

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