Skip to content

Lint for generalizeable captures #10530

Closed
@est31

Description

@est31

What it does

Take this code:

fn foo(v: Option<char>) -> u32 {
    match v {
        Some(w @ 'a' | w @ 'c' | w @ 'd') => w as u32,
        Some(_) => 1,
        None => 0,
    }
}

The binding w of the first arm's pattern is repeated three times. One can simplify the pattern as follows:

fn foo(v: Option<char>) -> u32 {
    match v {
        Some(w @ ('a' | 'c' | 'd')) => w as u32,
        Some(_) => 1,
        None => 0,
    }
}

Lint Name

repetitive_capture

Category

style

Advantage

This saves repetition of the binding (less chars) and makes the code more maintainable as renames only affect one site instead of multiple

Drawbacks

It adds one layer of ()s, but this should be bearable

Example

provided above

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions