Skip to content

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.

const minimum = Math.min(5, 10);
const maximum = Math.max(3, 7, 2);
const clamped = Math.max(0, Math.min(100, value));

This rule is not configurable.

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.

Made with ❤️‍🔥 around the world by the Flint team and contributors.