Skip to content

cast_abs_to_unsigned #8873

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

Closed
SwishSwushPow opened this issue May 23, 2022 · 0 comments · Fixed by #8876
Closed

cast_abs_to_unsigned #8873

SwishSwushPow opened this issue May 23, 2022 · 0 comments · Fixed by #8876
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

@SwishSwushPow
Copy link

Summary

The lint cast_abs_to_unsigned is showing a warning even when the variable is cast to usize. The recommended code change does not produce working code and in the end leads to a more convoluted solution. Maybe the lint should not be triggered by code that is casting to usize.

Lint Name

cast_abs_to_unsigned

Reproducer

I tried this code:

fn main() {
    let a: i32 = -3;
    let _: usize = a.abs() as usize; // this produces the `cast_abs_to_unsigned` warning

    // let _: usize = a.unsigned_abs(); // fails with "mismatched types, expected `usize`, found `u32`"
    // let _: usize = a.unsigned_abs().try_into().unwrap(); // this is then recommended
}

I saw this happen:

warning: casting the result of `i32::abs()` to usize
 --> src/main.rs:3:20
  |
3 |     let _: usize = a.abs() as usize;
  |                    ^^^^^^^^^^^^^^^^ help: replace with: `a.unsigned_abs()`
  |
  = note: `#[warn(clippy::cast_abs_to_unsigned)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_abs_to_unsigned

I expected to see this happen: I suppose the lint does not apply to such a line of code.

Version

rustc 1.61.0 (fe5b13d68 2022-05-18)

Additional Labels

No response

@SwishSwushPow SwishSwushPow 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 May 23, 2022
@bors bors closed this as completed in 9add456 May 31, 2022
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.

1 participant