Skip to content

single_match: type are incompatible #12758

@matthiaskrgr

Description

@matthiaskrgr

Summary

.

Reproducer

I tried this code:
cargo clippy --fix

fn test(s: &[u8]) {
    match &s[0..3] {
        b"hello" => {}
        _ => {}
    }
}

fn main() {}

I expected to see this happen:

Instead, this happened:

warning: function `test` is never used
 --> src/main.rs:1:4
  |
1 | fn test(s: &[u8]) {
  |    ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: you seem to be trying to use `match` for an equality check. Consider using `if`
 --> src/main.rs:2:5
  |
2 | /     match &s[0..3] {
3 | |         b"hello" => {}
4 | |         _ => {}
5 | |     }
  | |_____^ help: try: `if s[0..3] == b"hello" {}`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match
  = note: `#[warn(clippy::single_match)]` on by default

warning: `lint` (bin "lint" test) generated 2 warnings (run `cargo clippy --fix --bin "lint" --tests` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `lint`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0277]: can't compare `[u8]` with `&[u8; 5]`
 --> src/main.rs:2:16
  |
2 |     if s[0..3] == b"hello" {}
  |                ^^ no implementation for `[u8] == &[u8; 5]`
  |
  = help: the trait `std::cmp::PartialEq<&[u8; 5]>` is not implemented for `[u8]`
help: consider dereferencing here
  |
2 |     if s[0..3] == *b"hello" {}
  |                   +

Version

rustc 1.80.0-nightly (79734f1db 2024-05-02)
binary: rustc
commit-hash: 79734f1db8dbe322192dea32c0f6b80ab14c4c1d
commit-date: 2024-05-02
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions