Suggest usage of Ord::clamp
, f32::clamp
, and f64::clamp
to simplify code
#9477
Labels
A-lint
Area: New lints
Uh oh!
There was an error while loading. Please reload this page.
What it does
Identifies good opportunities for a
clamp
function fromstd
orcore
, and suggests using it.clamp
functionsf32::clamp
f64::clamp
Ord::clamp
Lint Name
clamping_without_clamp
Category
complexity
Advantage
It's much shorter and easier to read, also doesn't use any control flow.
Drawbacks
The clamp functions panic in some circumstances which the original patterns will merely malfunction on. Namely,
Ord
types will panic ifmax < min
and the floating point functions will additionally panic ifmin.is_nan()
ormax.is_nan()
. Some may consider this a perk, but I'm listing it as a drawback because it may introduce panics where there weren't any before.Example
Could all be written as:
The text was updated successfully, but these errors were encountered: