Skip to content

match_like_matches_macro should offer str comparison #14612

Open
@nyurik

Description

@nyurik

Summary

When the match statement is used with &str, I think it is better to suggest string comparison rather than a matches!(...) macro

Lint Name

match_like_matches_macro

Reproducer

I tried this code:

fn main() {
    assert!(cmp_str("abc"));
    assert!(cmp_str2("abc", 0));
}

fn cmp_str(v: &str) -> bool {
    match v {
        "a" | "bcd" => true,
        _ => false,
    }
}

fn cmp_str2(v: &str, i: usize) -> bool {
    match v {
        "abc" | "d" | "ef" if i == 0 => true,
        _ => false,
    }
}

I saw this happen:

warning: match expression looks like `matches!` macro
  --> src/main.rs:7:5
   |
7  | /     match v {
8  | |         "a" | "bcd" => true,
9  | |         _ => false,
10 | |     }
   | |_____^ help: try: `matches!(v, "a" | "bcd")`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro
   = note: `#[warn(clippy::match_like_matches_macro)]` on by default

warning: match expression looks like `matches!` macro
  --> src/main.rs:14:5
   |
14 | /     match v {
15 | |         "abc" | "d" | "ef" if i == 0 => true,
16 | |         _ => false,
17 | |     }
   | |_____^ help: try: `matches!(v, "abc" | "d" | "ef" if i == 0)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro

I expected to see this happen:

   |
   | |_____^ help: try: `v == "a" || v == "bcd"`
   |
...
   | |_____^ help: try: `(v == "a" || v == "bcd") && i == 0`
   |

Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-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