Skip to content

cast_sign_loss: value can't be negative if guarded by assert: #3089

Open
@matthiaskrgr

Description

@matthiaskrgr

similar to #2728 however, the value was not a constant in my original code, instead I protected against negative values with an assert().
clippy 0.0.212

#[cfg_attr(feature = "cargo-clippy", warn(cast_sign_loss))]

fn main() {
    let x = get_some_number();
    assert!(x > 0);
    let y = "hi".to_string().repeat(x as usize);
    println!("{}", y);
}

pub fn get_some_number() -> i32 {
	5
}
warning: casting i32 to usize may lose the sign of the value
 --> src/main.rs:6:37
  |
6 |     let y = "hi".to_string().repeat(x as usize);
  |                                     ^^^^^^^^^^
  |
note: lint level defined here
 --> src/main.rs:1:43
  |
1 | #[cfg_attr(feature = "cargo-clippy", warn(cast_sign_loss))]
  |                                           ^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#cast_sign_loss

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions