<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/189646>189646</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-tidy] Add `modernize-use-std-clamp` check
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy,
check-request
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
unterumarmung
</td>
</tr>
</table>
<pre>
A modernization check for canonical clamp-shaped code would still be useful in real C++17 codebases. The obvious target is nested `min`/`max` expressions such as `std::min(std::max(x, lo), hi)` and equivalent ternary ladders that are really just spelling `std::clamp(x, lo, hi)` by hand.
This pattern is easy to recognize once you know it, but in production code it is still noisier than the intent deserves. Hand-written clamp logic makes reviewers re-parse boundary handling, increases the chance of subtle inconsistencies between sites, and can hide bugs when one branch is edited incorrectly or the order of comparisons drifts. `std::clamp` expresses the operation directly and makes the code read as a bounded value rather than as control flow.
Typical “happy path” examples would be things like:
```cpp
auto y = std::min(std::max(x, lo), hi);
```
```cpp
value = std::max(lo, std::min(value, hi));
```
```cpp
return x < lo ? lo : (hi < x ? hi : x);
```
These are common, mechanically recognizable spellings of a clamp, and replacing them with `std::clamp` would usually improve readability immediately.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVV2O4zgPPI3yQiRw7I6TPOQh3f0F3wHmArLERJyWJY8oJfGcfkE7_TOLXmAXMGCZkKvIYlHSzHQJiAe1eVab14Uu2cV0KCFjKr1OfQmXRRfteDhCHy2mQL91phjAODRvcI4JjA4xkNEejNf9sGSnB7RgokW4xeItcCbvoUMojOfigQIk1B5eVP2s6uf1dtrcaUZewQ-HELsrxcKQdbpgBmIIyBktqLbqKai2UvVJ1vqu2grwPiRkphgYuBgHmmUnZ6uao2qO8ku9-_zUd1Xv7qp-AR9VvZeFI1m0FehgAX8VumqPIUPGFHQawWtrMTFkpzPohFMBfoSfhTPwgN5TuPxBOonxlecLSTe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