rust-lang / rust-lang/rust-clippy

Pedantic suggestion of using an array type definition

Open
#9,669 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Suggests to define a array type alias to simplify the code, making it more readable.

Lint Name

use_array_type_alias

Category

pedantic

Advantage

No response

Drawbacks

No response

Example

In this small program from the Benchmarks Game:
https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/mandelbrot-rust-6.html

There are lines of code like:

fn clr_pixels_nle(v : &[f64x2;4], f: f64, pix8 : &mut u8){
...
fn calc_sum(r: &mut [f64x2;4], i : &mut[f64x2;4], sum : &mut[f64x2;4], init_r: &[f64x2;4], init_i : f64x2){
...
fn mand8(init_r: &[f64x2;4], init_i : f64x2) -> u8 {
...

I suggest to add a lint in clippy::pedantic that suggests to define something like:

type Array1 = [f64x2; 4];

And then use that type in the above code, like:

fn clr_pixels_nle(v: &Array1, f: f64, pix8: &mut u8) {
...
fn calc_sum(r: &mut Array1, i: &mut Array1, sum: &mut Array1, init_r: &Array1, init_i: f64x2) {
...
fn mand8(init_r: &Array1, init_i: f64x2) -> u8 {
...

Defining type aliases to simplify array arguments in Rust code is quite under-used.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposed clippy::pedantic lint named use_array_type_alias and review the Rust examples in the issue, especially the repeated [f64x2; 4] arguments. Define the applicability and expected diagnostic, then add the lint and tests showing when the array type alias suggestion is emitted.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.