Skip to content

x² shouldn't emit cast_sign_loss #11642

@ShE3py

Description

@ShE3py

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

Metadata

Metadata

Assignees

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