Skip to content

redundant_guards does not take const fn-ness into account #12243

@smalis-msft

Description

@smalis-msft

Summary

When inside a const fn there are additional restrictions that redundant_guards appears to not consider.

Lint Name

redundant_guards

Reproducer

I tried this code:

pub const fn handle(x: &str) -> u32 {
    match x {
        y if y.is_empty() => 2,
        _ => 3,
    }
}

I saw this happen:

warning: redundant guard
 --> src/lib.rs:3:14
  |
3 |         y if y.is_empty() => 2,
  |              ^^^^^^^^^^^^
  |
  = 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 -         y if y.is_empty() => 2,
3 +         "" => 2,
  |

However applying that suggestion results in:

error[E0015]: cannot match on `str` in constant functions
 --> src/lib.rs:3:9
  |
3 |         "" => 2,
  |         ^^
  |
  = note: `str` cannot be compared in compile-time, and therefore cannot be used in `match`es
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

Version

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-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