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

manual_clamp suggests using clamp() in a const function #10474

Closed
matejcik opened this issue Mar 10, 2023 · 0 comments · Fixed by #10479
Closed

manual_clamp suggests using clamp() in a const function #10474

matejcik opened this issue Mar 10, 2023 · 0 comments · Fixed by #10479
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

@matejcik
Copy link

Summary

I'm manually reimplementing clamp() because the built-in version is not const. The manual_clamp rule notifies me about this.
It would be nice if it could recognize that i'm const and therefore the suggestion is not applicable.

Lint Name

manual_clamp

Reproducer

I tried this code:

#[deny(clippy::nursery)]
const fn clamp(x: u32, min: u32, max: u32) -> u32 {
    if x < min {
        min
    } else if x > max {
        max
    } else {
        x
    }
}

I saw this happen:

error: clamp-like pattern without using clamp function
 --> src/main.rs:3:5
  |
3 | /     if x < min {
4 | |         min
5 | |     } else if x > max {
6 | |         max
7 | |     } else {
8 | |         x
9 | |     }
  | |_____^ help: replace with clamp: `x.clamp(min, max)`

I expected to see this happen:

no warning

Version

rustc 1.70.0-nightly (39f2657d1 2023-03-09)
binary: rustc
commit-hash: 39f2657d1101b50f9b71ae460b762d330cc8426b
commit-date: 2023-03-09
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 15.0.7

Additional Labels

No response

@matejcik matejcik 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 Mar 10, 2023
@bors bors closed this as completed in 8e1dd06 Mar 10, 2023
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