rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.34k stars 1.53k forks source link

Reduce `match` guard with equality with straightforward `match` #11697

Open frewsxcv opened 11 months ago

frewsxcv commented 11 months ago

What it does

Prefer matching on the value directly instead of using a guard with equality

Advantage

No response

Drawbacks

No response

Example

match x {
    y if y == 100 => ...,
}

Could be written as:

match x {
    100 => ...,
}
y21 commented 11 months ago

This lint already exists: redundant_guards

Your example code has this warning:

warning: redundant guard
 --> src/main.rs:4:14
  |
4 |         y if y == 100 => {},
  |              ^^^^^^^^
  |
  = 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
  |
4 -         y if y == 100 => {},
4 +         100 => {},
frewsxcv commented 11 months ago

This didn't cause a warning for me when using bitflags:

Screenshot 2023-10-22 at 7 53 50 PM
frewsxcv commented 11 months ago

And yes I have clippy lints turned on in my VSCode

y21 commented 11 months ago

Looks like a false negative then. From a quick glance, it looks like clippy doesn't consider a path to a const (GeomType::POINT) as an expression that can be a pattern, even though it really could be.

Smaller repro that doesn't emit any warnings, without the bitflags dep:

const X: i32 = 1;
match 1 {
    x if x == X => {}
    _ => {}
}
asquared31415 commented 11 months ago

Note that not all consts can be patterns and the precise semantics are being worked out. https://github.com/rust-lang/lang-team/issues/220 Though in this case I would be surprised if anything stopped a const like this from working as a pattern.