Skip to content

match_like_matches_macro should not trigger if match arms have comments #9164

@lopopolo

Description

@lopopolo

Summary

match_like_matches_macro suggests turning match expressions to a matches! expression even if doing so results in the loss of comments and documentation on each match arm.

Lint Name

match_like_matches_macro

Reproducer

I tried this code:

fn y(c: char) -> bool {
    match c {
        // numbers are bad!
        '1' | '2' | '3' => true,
        // spaces are very important to be true.
        ' ' => true,
        // as are dots
        '.' => true,
        _ => false,
    }
}

fn main() {
    dbg!(y('g'));
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: match expression looks like `matches!` macro
  --> src/main.rs:2:5
   |
2  | /     match c {
3  | |         // numbers are bad!
4  | |         '1' | '2' | '3' => true,
5  | |         // spaces are very important to be true.
...  |
9  | |         _ => false,
10 | |     }
   | |_____^ help: try this: `matches!(c, '1' | '2' | '3' | ' ' | '.')`
   |
   = note: `#[warn(clippy::match_like_matches_macro)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro

warning: `playground` (bin "playground") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.43s

I expected to see this happen: comments are preserved or the lint is not triggered.

Version

rust 1.62.0 (playground)

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions