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

Reduce match guard with equality with straightforward match #11697

Open
frewsxcv opened this issue Oct 22, 2023 · 5 comments
Open

Reduce match guard with equality with straightforward match #11697

frewsxcv opened this issue Oct 22, 2023 · 5 comments
Labels
A-lint Area: New lints

Comments

@frewsxcv
Copy link
Member

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 => ...,
}
@frewsxcv frewsxcv added the A-lint Area: New lints label Oct 22, 2023
@y21
Copy link
Member

y21 commented Oct 22, 2023

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
Copy link
Member Author

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

Screenshot 2023-10-22 at 7 53 50 PM

@frewsxcv
Copy link
Member Author

And yes I have clippy lints turned on in my VSCode

@y21
Copy link
Member

y21 commented Oct 23, 2023

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants