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

x² shouldn't emit cast_sign_loss #11642

Closed
ShE3py opened this issue Oct 8, 2023 · 2 comments · Fixed by #11883
Closed

x² shouldn't emit cast_sign_loss #11642

ShE3py opened this issue Oct 8, 2023 · 2 comments · Fixed by #11883
Assignees
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

@ShE3py
Copy link
Contributor

ShE3py commented Oct 8, 2023

Summary

Hi,

Clippy shoudn't lint about casting x2n as an unsigned integer since the result cannot be negative. The lint should still be shown for x3.

Lint Name

cast_sign_loss

Reproducer

I tried this code:

#[forbid(clippy::cast_sign_loss)]
pub fn square(x: i16) -> u32 {
    let x = x as i32;
    
    (x * x) as u32
}

I saw this happen:

error: casting `i32` to `u32` may lose the sign of the value
 --> src/lib.rs:6:5
  |
6 |     (x * x) as u32
  |     ^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss

I expected to see this happen:

No output, as x² ⩾ 0.

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2

Additional Labels

No response

@ShE3py ShE3py 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 Oct 8, 2023
@blyxyas
Copy link
Member

blyxyas commented Oct 8, 2023

@rustbot claim

@blyxyas blyxyas removed their assignment Oct 12, 2023
@J-ZhengLi
Copy link
Member

@rustbot claim

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.

3 participants