Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Supreeeme opened this issue Nov 13, 2024 · 0 comments · Fixed by #13698
Closed

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

Supreeeme opened this issue Nov 13, 2024 · 0 comments · Fixed by #13698
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Supreeeme
Copy link

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

@Supreeeme Supreeeme added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 13, 2024
github-merge-queue bot pushed a commit that referenced this issue Nov 17, 2024
#13698)

Two changes to `redundant_guards`:

- Lint float literals. We used to do that before but that was changed in
#11305 because at the time there was a future compat warning and it was
planned to make pattern matching floats a hard error.

In rust-lang/rust#116284 it was decided to actually remove the lint and
only make matching `NAN` specifically a hard error. The `NAN` part isn't
relevant/important here because this PR only changes what literals are
warned and `f64::NAN` isn't a literal, but I've added a test anyway to
make sure we continue to not lint there.
- Don't lint CStr literals because that can't be a pattern right now
(fixes #13681)

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant