Skip to content

clamp on std integer types should probably have a better error message and ideally track caller #142309

Open
@Morgane55440

Description

@Morgane55440

when running this code

fn main() {
    println!("{}", 0u8.clamp(7,3))
}

i get the following output

thread 'main' panicked at /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs:1078:9:
assertion failed: min <= max

this is very unhelpful in any non-trivial codebase that uses this method.
this is a pretty niche error but it can happen.

something like

thread 'main' panicked at src/main.rs:3:20:
invalid clamp range : there are no numbers smaller than 7 and greater than 3

would be ideal, but any improvement would be good.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions