Skip to content

New Lint: Use rest pattern instead of ignoring values when destructuring structs and enums #10666

Open
@wainwrightmark

Description

@wainwrightmark

What it does

This lint will use a rest pattern (..), instead of ignoring values with _ when destructuring structs and enums.

I'm suggesting to only do this for _, and leave as they are the cases where the values are named and prefixed with an underscore.

Lint Name

ignored_field_when_destructuring

Category

style

Advantage

  • This reduces noise and makes it more obvious which fields are relevant, especially when there are many ignored values.

Drawbacks

  • This will prevent a compilation error if additional fields are added to the struct. Some programmers may want that error to remind them to do something with the newly added fields.

Example

struct Point {
    x: i32,
    y: i32,
}

let origin = Point { x: 0, y: 0 };
let Point { x, y: _ } = origin;

println!("x is {}", x);

Could be written as:

struct Point {
    x: i32,
    y: i32,
}

let origin = Point { x: 0, y: 0 };
let Point { x, .. } = origin;

println!("x is {}", x);

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