rust-lang / rust-lang/rust-clippy

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

Open
#3,089 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

similar to https://github.com/rust-lang-nursery/rust-clippy/issues/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides no repository file or test entry point. Start by reproducing the Rust example and compare the requested behavior with rust-clippy issue 2728. Done means the cast_sign_loss lint accounts for the preceding assert before warning about the cast.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.