unnecessaryMathClamps
Reports unnecessary Math.min and Math.max calls with constant arguments or incorrect clamping patterns.
✅ This rule is included in the ts logical presets.
Math.min and Math.max are commonly used to constrain values to specific ranges.
However, when all arguments are constant values, the result is always predictable and should be replaced with the constant directly.
Additionally, nested Math.min/max calls are often accidentally swapped to never return a value in-range
This rule reports on Math.min and Math.max calls that don’t correctly clamp.
Examples
Section titled “Examples”const minimum = Math.min(5, 10);const maximum = Math.max(3, 7, 2);const clamped = Math.max(0, Math.min(100, value));const minimum = 5;const maximum = 7;const clamped = Math.min(100, Math.max(0, value));Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you have a codebase that intentionally uses Math.min/max with constants for code generation or readability purposes, you might choose to disable this rule. However, in most cases, replacing constant Math operations with their computed results makes the code more efficient and clearer.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noConstantMathMinMaxClamp - Oxlint:
oxc/bad-min-max-func