rust-lang / rust-lang/rust-clippy

max(0) false positive for cast_sign_loss

Open
#11,799 1 comment 1 reaction 1 assignee View on GitHub

@dswij is already working on this.

Since Nov 13, 2023.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary
signed.max(0) as u64

shouldn't warn about loss of sign.

Lint Name

cast_sign_loss

Reproducer

I tried this code:

    let i = -1_i64;
    let _u = i.max(0) as u64;

I saw this happen:

warning: casting `i64` to `u64` may lose the sign of the value

I expected to see this happen:

  • no warning

Same with i.clamp(0, 999) as u64.

Version
rustc 1.76.0-nightly (2b603f95a 2023-11-12)
binary: rustc
commit-hash: 2b603f95a48f10f931a61dd208fe3e5ffd64e491
commit-date: 2023-11-12
host: aarch64-apple-darwin
release: 1.76.0-nightly
LLVM version: 17.0.4
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.