oxc/bad-min-max-func
Disallow Math.min and Math.max combinations that always give the same result
Math.min(Math.max(value, low), high) keeps a value between two limits. If you swap the limits, the result no longer depends on the value. Math.min(Math.max(value, 100), 0) is 0 for every value, because the inner call is at least 100.
The rule compares the number literals in a Math.min call with those in a Math.max call nested directly inside it, or the other way around. Limits that are variables aren't checked.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"oxc/bad-min-max-func" = "error" # or "warn"Examples#
example.js2 problems
const percent = Math.min(Math.max(input, 100), 0)Math.min and Math.max combination leads to constant result;
const channel = Math.max(255, Math.min(0, red))Math.min and Math.max combination leads to constant result;example.jsNo problems
const percent = Math.min(Math.max(input, 0), 100);
const channel = Math.max(0, Math.min(255, red));
const page = Math.min(Math.max(requested, first), last);