rust-lang / rust-lang/rust-clippy

Non-saturating arithmetic on saturating types

Open
#15,328 1 comment 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

Ensures that Saturating types aren't accidentally misused.

I've had a tuple with a Saturating<u8> field, and I've written num.0 += 1 thinking I'm accessing a field of my tuple, but actually it was the number itself, and I accidentally accessed the inner value of the Saturating wrapper, and ended up having overflowing arithmetic instead.

Advantage

Ensures the wrapper type isn't accidentally bypassed. The whole point of the wrapper is to ensure it's used.

Drawbacks

Someone might want to perform direct arithmetic on the inner type when knowing it won't overflow, but in that case maybe an explicit allow(lint) wouldn't be too bad to communicate the intention. Or they could just use the wrapper type when the arithmetic is obviously in bounds and the optimizer can remove range checks.

Example
    let mut n = Saturating(0u8);
    n.0 += 100;

Could be written as:

    let mut n = Saturating(0u8);
    n += 100;

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 with the issue's Saturating examples and clarify the intended lint behavior for direct field arithmetic versus wrapper arithmetic. Then locate the relevant Clippy lint entry point and its tests; done means the misuse is detected while the explicitly wrapper-based form remains valid.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.