Skip to content

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

Closed
@matejcik

Description

@matejcik

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions