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

Suggest usage of array.as_slice() (instead of &array[..]) and array.as_mut_slice() #7633

Open
Luro02 opened this issue Sep 4, 2021 · 5 comments
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-restriction Lint: Belongs in the restriction lint group

Comments

@Luro02
Copy link

Luro02 commented Sep 4, 2021

What it does

Lints for example this code:

let array: [u8; 4] = [1, 2, 3, 4];
let slice: &[u8] = &array[..];

and suggests array::as_slice:

let array: [u8; 4] = [1, 2, 3, 4];
let slice: &[u8] = array.as_slice();

(same should be suggested for &mut array[..], which can be replaced with array.as_mut_slice())

Categories (optional)

  • Kind: clippy::pedantic or clippy::style

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

For example:

Drawbacks

  • might have a lot of false positives or not much benefit in using the function

Possibly related issues:

Stabilization

At the time of writing this issue, the function is still unstable, but with the merging of rust-lang/rust#88353, this feature will be stabilized. (final-comment-period, so will be merged in the next few days)

@Luro02 Luro02 added the A-lint Area: New lints label Sep 4, 2021
@giraffate giraffate added the good-first-issue These issues are a good way to get started with Clippy label Sep 7, 2021
@ghost
Copy link

ghost commented Sep 7, 2021

Not sure about this one. We currently suggest for x in &y { .. } over for x in y.iter() { .. } - explicit_iter_loop. This seems like it's going to the opposite direction in terms of style. Also we don't current recommend Vec::as_slice over slicing although we could have for years.

@camsteffen camsteffen added the L-restriction Lint: Belongs in the restriction lint group label Sep 7, 2021
@Dmitry-Borodin
Copy link
Contributor

Should this issue be implemented or it's doubtable?
I'm looking for a good first issue, and I would like to understand if this should be implemented as in original message and under which category it's better to place.

@giraffate
Copy link
Contributor

IMO it looks like a good idea to implement this, but the restriction category might be better.

@nindalf
Copy link
Contributor

nindalf commented Feb 8, 2022

@rustbot claim

@nindalf
Copy link
Contributor

nindalf commented Feb 9, 2022

@rustbot release-assignment

As mentioned by @camsteffen in my PR, it's probably better if @Jarcho adds this lint to their PR #8218 or as a follow up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-restriction Lint: Belongs in the restriction lint group
Projects
None yet
Development

No branches or pull requests

5 participants