Skip to content

assist: Replace if matches!(..) with match expr #20013

@g-plane

Description

@g-plane

Can we add an assist which can replace if matches!(..) with match expression like this?

From

if matches!(expr, pat) {
    a
} else {
    b
}

to

match expr {
    pat => {
        a
    }
    _ => {
        b
    }
}

My thought is to expand the matches! macro, but whitespaces and comments will be lost when expanding. For example:

if matches!(compute(arg1, /* comment*/ arg2), Enum::A | Enum::B) {
    // ...
}

will become

match compute(arg1,arg2) {
    Enum::A|Enum::B => {
        // ...
    }
    _ => {
        // ...
    }
}

Is this acceptable? Or, are there any better solutions for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions