<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/146294>146294</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-tidy] Check request: misc-no-template-recursion
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
denzor200
</td>
</tr>
</table>
<pre>
clang-tidy already has [misc-no-recursion](https://clang.llvm.org/extra/clang-tidy/checks/misc/no-recursion.html) check that detects recursive function calls in non-template code. However, in C++, recursion can also occur at compile time, via recursive template instantiation, which can lead to:
- Significantly increased compilation time;
- Inflated binary file size due to multiple instantiations;
- Compiler stack overflow.
Need a check that will discover direct and indirect recursion in templates.
Example direct:
```
template<int N> struct Factorial {
static const int value = N * Factorial<N-1>::value; // BAD
};
```
Example indirect (B → C → B):
```
template<typename T> struct B { using Type = typename C<T>::Type; }; // BAD
template<typename T> struct C { using Type = typename B<T>::Type; }; // BAD
```
All the samples above were simplified to not have the base case.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVN2O8jYQfRpzMwIlDgFykQsSFrU3e9N9gYk9EPdzbGpP4GOfvnL4WbpS20-KBJHPzDlnMj4Yozk6olqUjSh3Mxy596HW5D59kFk267y-1sqiO87Z6CugDYT6Cj1GEGUzmKjmzs8DqTFE450od0JueuZTFMVWyL2Q-6l8Ye15WPhwFHJPPzng42Dqm156Uj-ikPvUU8j9a9dFz4MVsoIJBNwjgyYmxRHuoDPBYXSKjXeg0NoIxoHzbs40nCwygfKaFvCbv9CZgpBtArRCNtPTwpMMFDpAGz14pcYAyKD8cDKWgM1ACXs2-ML7ZDAuMjo2mFQk3KU3qp_6WUIN7NNMsu0c_jBHZw5GoWN7BeNUIIyk70RT_Y2saCb87-6QGDR0xmG4wiGpieaTQI8E7GEYLZuT_aYhPurbm4EAkVH9AH-mcLD-shBZ0vNOpAFfh3sx1oI2USUkaBNIMaDTYNz95Wtcxj0nEO8N337ikMTcsDfTYpXdn2z7wIuiNY7hXRRvEDmMimGPin0waEGsk3YASKLZKFDeRYZUcEY7EohiB-8g5ParSBTt-zwXxVviLLYTThQNwG0TodnukpT17jaYV00vsp8mhdw0IN6k2KxEJaF9-d8IWYliC__ujK8ncjgQfLzYa5ItGKNxR_i4nm4mnshWFO3HU306T-JvcuGbif9jav-TqflVpu8GRbbdWgvcE8RpWhGw82eCC4W0ksPJmoOhtOzgPEOP6Yb0BB1GAoWR7jsy03Whq6LCGdX5uszlMivyfNbXmKtSH5bLZbXZqGWXqa6j1bqS63KzItx0M1PLTJbZSlayKDZFvigol-V6rUnpvJKHTCwzGtDYZ-bMTIwj1flyJavlzGJHNk6RJ-VrBskUgaFOVfNuPEaxzKyJHL_6sGE7heVLWbmDdro5gf4aKaZ1h0cuPj7SV5TNxmDrf-bj0XA_dgvlByH3ier-Mz8F_2e6P3I_6U_heLdwruXfAQAA__9YZNTK">