noConstantMathMinMaxClamp
Summary
Section titled “Summary”- Rule available since:
v1.7.0
- Diagnostic Category:
lint/correctness/noConstantMathMinMaxClamp
- This rule is recommended, which means is enabled by default.
- This rule has an unsafe fix.
- The default severity of this rule is error.
- Sources:
- Same as
min_max
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "correctness": { "noConstantMathMinMaxClamp": "error" } } }}
Description
Section titled “Description”Disallow the use of Math.min
and Math.max
to clamp a value where the result itself is constant.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”Math.min(0, Math.max(100, x));<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-constant-math-min-max-clamp">lint/correctness/noConstantMathMinMaxClamp</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━<br /><br /> <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">This </span><span style="color: Tomato;"><strong>Math.min/Math.max</strong></span><span style="color: Tomato;"> combination leads to a constant result.</span><br /> <br /> <strong><span style="color: Tomato;">></span></strong> <strong>1 │ </strong>Math.min(0, Math.max(100, x));<br /> <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><br /> <strong>2 │ </strong><br /> <br /> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">It always evaluates to </span><span style="color: lightgreen;"><strong>0</strong></span><span style="color: lightgreen;">.</span><br /> <br /> <strong><span style="color: Tomato;">></span></strong> <strong>1 │ </strong>Math.min(0, Math.max(100, x));<br /> <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><br /> <strong>2 │ </strong><br /> <br /> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Swap </span><span style="color: lightgreen;"><strong>0</strong></span><span style="color: lightgreen;"> with </span><span style="color: lightgreen;"><strong>100</strong></span><span style="color: lightgreen;">.</span><br /> <br /> <strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">M</span><span style="color: Tomato;">a</span><span style="color: Tomato;">t</span><span style="color: Tomato;">h</span><span style="color: Tomato;">.</span><span style="color: Tomato;">m</span><span style="color: Tomato;">i</span><span style="color: Tomato;">n</span><span style="color: Tomato;">(</span><span style="color: Tomato;"><strong>0</strong></span><span style="color: Tomato;">,</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">M</span><span style="color: Tomato;">a</span><span style="color: Tomato;">t</span><span style="color: Tomato;">h</span><span style="color: Tomato;">.</span><span style="color: Tomato;">m</span><span style="color: Tomato;">a</span><span style="color: Tomato;">x</span><span style="color: Tomato;">(</span><span style="color: Tomato;"><strong>1</strong></span><span style="color: Tomato;"><strong>0</strong></span><span style="color: Tomato;"><strong>0</strong></span><span style="color: Tomato;">,</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">x</span><span style="color: Tomato;">)</span><span style="color: Tomato;">)</span><span style="color: Tomato;">;</span><br /> <strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">M</span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;">h</span><span style="color: MediumSeaGreen;">.</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;">i</span><span style="color: MediumSeaGreen;">n</span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;"><strong>1</strong></span><span style="color: MediumSeaGreen;"><strong>0</strong></span><span style="color: MediumSeaGreen;"><strong>0</strong></span><span style="color: MediumSeaGreen;">,</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">M</span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;">h</span><span style="color: MediumSeaGreen;">.</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">x</span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;"><strong>0</strong></span><span style="color: MediumSeaGreen;">,</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">x</span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;">;</span><br /> <strong>2</strong> <strong>2</strong><strong> │ </strong> <br /> <br /></code></pre>
Math.max(100, Math.min(0, x));<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-constant-math-min-max-clamp">lint/correctness/noConstantMathMinMaxClamp</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━<br /><br /> <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">This </span><span style="color: Tomato;"><strong>Math.min/Math.max</strong></span><span style="color: Tomato;"> combination leads to a constant result.</span><br /> <br /> <strong><span style="color: Tomato;">></span></strong> <strong>1 │ </strong>Math.max(100, Math.min(0, x));<br /> <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><br /> <strong>2 │ </strong><br /> <br /> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">It always evaluates to </span><span style="color: lightgreen;"><strong>100</strong></span><span style="color: lightgreen;">.</span><br /> <br /> <strong><span style="color: Tomato;">></span></strong> <strong>1 │ </strong>Math.max(100, Math.min(0, x));<br /> <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><br /> <strong>2 │ </strong><br /> <br /> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Swap </span><span style="color: lightgreen;"><strong>100</strong></span><span style="color: lightgreen;"> with </span><span style="color: lightgreen;"><strong>0</strong></span><span style="color: lightgreen;">.</span><br /> <br /> <strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">M</span><span style="color: Tomato;">a</span><span style="color: Tomato;">t</span><span style="color: Tomato;">h</span><span style="color: Tomato;">.</span><span style="color: Tomato;">m</span><span style="color: Tomato;">a</span><span style="color: Tomato;">x</span><span style="color: Tomato;">(</span><span style="color: Tomato;"><strong>1</strong></span><span style="color: Tomato;"><strong>0</strong></span><span style="color: Tomato;"><strong>0</strong></span><span style="color: Tomato;">,</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">M</span><span style="color: Tomato;">a</span><span style="color: Tomato;">t</span><span style="color: Tomato;">h</span><span style="color: Tomato;">.</span><span style="color: Tomato;">m</span><span style="color: Tomato;">i</span><span style="color: Tomato;">n</span><span style="color: Tomato;">(</span><span style="color: Tomato;"><strong>0</strong></span><span style="color: Tomato;">,</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">x</span><span style="color: Tomato;">)</span><span style="color: Tomato;">)</span><span style="color: Tomato;">;</span><br /> <strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">M</span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;">h</span><span style="color: MediumSeaGreen;">.</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">x</span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;"><strong>0</strong></span><span style="color: MediumSeaGreen;">,</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">M</span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;">h</span><span style="color: MediumSeaGreen;">.</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;">i</span><span style="color: MediumSeaGreen;">n</span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;"><strong>1</strong></span><span style="color: MediumSeaGreen;"><strong>0</strong></span><span style="color: MediumSeaGreen;"><strong>0</strong></span><span style="color: MediumSeaGreen;">,</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">x</span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;">;</span><br /> <strong>2</strong> <strong>2</strong><strong> │ </strong> <br /> <br /></code></pre>
Math.min(100, Math.max(0, x));
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.