Skip to content

redundant_guards suggests change that doesn't compile for matching on CStr #13681

Closed
@Supreeeme

Description

@Supreeeme

Summary

Clippy emits a suggestion that doesn't compile when matching on a CStr.

Lint Name

redundant_guards

Reproducer

I tried this code:

fn main() {
    match c"hi" {
        x if x == c"hi" => (),
        _ => ()
    }
}

I saw this happen:

warning: redundant guard
 --> src/main.rs:3:14
  |
3 |         x if x == c"hi" => (),
  |              ^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_guards
  = note: `#[warn(clippy::redundant_guards)]` on by default
help: try
  |
3 -         x if x == c"hi" => (),
3 +         c"hi" => (),
  |

Making this change does not compile:

error: cannot use unsized non-slice type `CStr` in constant patterns
 --> src/main.rs:3:9
  |
3 |         c"hi" => (),
  |         ^^^^^

Version

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

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