Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ER] Useless slicing warning #8089

Open
leonardo-m opened this issue Dec 7, 2021 · 4 comments
Open

[ER] Useless slicing warning #8089

leonardo-m opened this issue Dec 7, 2021 · 4 comments
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@leonardo-m
Copy link

What it does

Inspired by this issue:
rust-lang/rust#91625

In code like this:

fn main() {
    let s = "abc".to_string();
    let t = s.strip_prefix(&['a', 'x'][..]);
    println!("{:?}", t);
}

Clippy could suggest to write instead:

s.strip_prefix(&['a', 'x'])

Or even:

s.strip_prefix(['a', 'x'])

Lint Name

No response

Category

No response

Advantage

No response

Drawbacks

No response

Example

<code>

Could be written as:

<code>
@leonardo-m leonardo-m added the A-lint Area: New lints label Dec 7, 2021
@est31
Copy link
Member

est31 commented Dec 7, 2021

Another example where there should be a lint:

fn foo(_v: &[u8]) {}

fn main() {
    let v = vec![1, 2, 3, 4];
    foo(&v[..]);
}

@giraffate
Copy link
Contributor

We have the redundant_slicing lint, so it would be better to improve this.

@giraffate giraffate added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages and removed A-lint Area: New lints labels Dec 7, 2021
@giraffate
Copy link
Contributor

Clippy could suggest to write instead:

s.strip_prefix(&['a', 'x'])

It causes a compile error with currently stable 1.57.0, not beta and nightly.

@est31
Copy link
Member

est31 commented Dec 8, 2021

FTR, this doesn't trigger the lint either:

#![forbid(clippy::redundant_slicing)]
fn foo(_v: &[u8]) {}

fn main() {
    foo(&[1, 2, 3, 4][..]);
}

And the version without the [..] compiles on stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages
Projects
None yet
Development

No branches or pull requests

3 participants