rust-lang / rust-lang/rust-clippy

`restriction` lints for undesired method of handling overflow

Open
#15,766 2 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

wrap_on_overflow would raise a warning if wrapping_add (or similar methods) are invoked.

saturate_on_overflow would raise a warning if saturating_add (or similar methods) are invoked.

Advantage

In my work, I regularly control the behavior on overflow, but I prefer using saturating_*. Unfortunately, for whatever reason, I continue to accidentally type (and thankfully seem to catch) wrapping_*. I'm concerned for the day I write wrapping_ and DON'T catch it. I also have yet to want, in my repository, wrap on overflow and accordingly am fine banning it entirely (without the allow pragma).

I believe a pedantic lint to explicitly declare saturation vs wrap as the preferred way to handle overflow is worthwhile accordingly. This would be approximate to the existing lints establishing preferences on endianess.

Drawbacks

No response

Example
a.wrapping_add(b)

Could be written as:

a.saturating_add(b)
Comparison with existing lints

https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects already exists to require a mode be specified, but it doesn't allow declaring a preferred mode.

The premise is comparable to https://rust-lang.github.io/rust-clippy/master/index.html#big_endian_bytes, https://rust-lang.github.io/rust-clippy/master/index.html#host_endian_bytes, https://rust-lang.github.io/rust-clippy/master/index.html#little_endian_bytes which allow declaring a preferred mode to represent bytes.

Additional Context

No response

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 Clippy's existing arithmetic_side_effects and endian-preference lints, using the linked lint documentation as context for how preferences are declared. Determine how wrapping_* and saturating_* methods should be covered and how the preferred mode should be configured. Done means the new pedantic lint consistently warns on the undesired overflow method and documents the supported behavior with examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
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.