rust-lang / rust-lang/rust-clippy

lint suggestion: `suspicious_mixed_type_operation`

Open
#17,290 0 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

Bit-related functions return u32 , this allows to compile some "suspicious" operations from mismatching calling types. which can leads to overflows like:

let x = u8::BITS - 5u64.leading_zeros();

let x: u16 = 0b1111111111111111;
let _ = u8::BITS - x.trailing_ones();

let mask = 1u32 << 5u64.leading_zeros();
Advantage
  • Avoids overflows
  • In the cases where it does not overflow, Improves readability
Drawbacks

The lint should be black list specific cases, which are clearly misleading. Otherwise, if the cases are kept to general there is the risk of triggering for false positives / true negatives.

Example
let x = u8::BITS - 5u64.leading_zeros();

let x: u16 = 0b1111111111111111;
let _ = u8::BITS - x.trailing_ones();

let mask = 1u32 << 5u64.leading_zeros();

Could be written as:

let x = u8::BITS - 5u8.leading_zeros();

let x: u16 = 0b1111111111111111;
let _ = u16::BITS - x.trailing_ones();

let mask = 1u32 << 5u32.leading_zeros();
Comparison with existing lints

No response

Additional Context

This lint was inspired with the conversation on: https://github.com/rust-lang/rust-clippy/pull/16902#discussion_r3412438731

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

Start by reading the linked rust-lang/rust-clippy pull request discussion and reviewing the mixed-type examples in this issue. Define which suspicious operations the lint should blacklist while avoiding false positives, compare the proposal with existing lints, and consider the work complete when that scope is agreed and the examples are covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.