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

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

Open
matthiaskrgr opened this issue Aug 27, 2018 · 0 comments
Open

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

matthiaskrgr opened this issue Aug 27, 2018 · 0 comments

Comments

@matthiaskrgr
Copy link
Member

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant