rust-lang/rust-clippy

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

Open

#7,633 创建于 2021年9月4日

在 GitHub 查看
 (6 评论) (2 反应) (1 负责人)Rust (1,391 fork)batch import
A-lintL-restrictiongood first issue

仓库指标

Star
 (10,406 star)
PR 合并指标
 (平均合并 19天 22小时) (30 天内合并 113 个 PR)

描述

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:

  • easier to read for people new to rust (not very obvious what [..] does)
  • there must be a reason why array::as_slice exists? (see rust-lang/rust#76120)

Drawbacks

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

Possibly related issues:

  • #728
  • #5598

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)

贡献者指南