rust-lang / rust-lang/rust-clippy

New lint that suggests the use of mixed type operations like wrapping_sub_unsigned

Open
#13,715 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What it does

Suggests to replace code that contains an as cast, with specialized functions from the std library.

Advantage

Makes the intent more explicit.

Drawbacks

Slightly longer code.

Example
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
#![warn(clippy::style)]
fn main() {
    let a = 1_i64;
    let b = 2_u64;
    let c = 2_u32;
    let x = a - (b as i64);
    let y = a.wrapping_sub_unsigned(b);
    let z = a.wrapping_sub_unsigned(u64::from(c));
    println!("{x:?} {y:?} {z:?}");
}

This code gives a clippy::cast_possible_wrap warning on the (b as i64) part, but I think it should suggest to replace code like in the computation of x, with the code that computes y. Similar suggestions could be used for other mixed-type operations.

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

No source file or test is named. Start by reviewing the existing clippy::cast_possible_wrap lint and the mixed-type operations in the example; done means a lint can identify applicable casts and suggest the corresponding specialized standard-library operations, with coverage for the described cases.

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.