<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/84326>84326</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
add clang-tidy check to suggest usage of `const_iterator` if possible
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
firewave
</td>
</tr>
</table>
<pre>
It be helpful to have a check which suggests to use `const_iterator` and/or the appropriate functions which provide it - if possible.
```cpp
#include <map>
std::map<int, int> cb();
void f()
{
std::map<int, int> m = cb();
std::map<int, int>::iterator I1 = m.begin(); // suggest const_iterator and/or cbegin()
auto I2 = m.begin(); // suggest cbegin()
std::map<int, int>::iterator it1 = m.find(0); // suggest const_iterator
auto it2 = m.find(0); // requires std::as_const() - not in scope - separate check?
auto I3 = m.cbegin(); // no warning
auto it3 = std::as_const(m).find(0); // no warning
}
```
This might make the suggestions of `modernize-use-auto` less prone to remove constness as outlined in #84324. This might also enable `misc-const-correctness` to report more cases.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVMFu4zgM_Rr5QjhQZCdxDj502gnQ-94HskTb3JElryinmP36hRx3mt1pi1nAsADRfOR75qNmpsEjtuLwRRyeCr2kMcS2p4gv-opFF-yP9jlBhzCim_vFQQow6iuCBjOi-Q4vI5kReBkG5MQ5vDCCOEoTPKdvlDDqFKI4StDeCnUJEdKIoOc5hjmSTgj94k2i4HlDm2O4kkWgBCVQD3Ngps7hTsgnIR-291HeHjPP242qyBu3WARRPU56FtXX-wxOVlQPonpYQ4_kk1CPkI_qK5hOqEaos6i-3OdcA1not9Dt_rR9AADwKeQEonr6CPjT1FvgVTt43q9I067DgfxPOBDqItTlVXz4t-Jvcpv7tPsWAEAvKcCz-j38j3D-BxVKr1x6yv018ne4vNs1JfUpVMS_ForIb-1p_rbi3ihACT4kIA9swoxQAuOsYx7IdbRFdXlfrWora96Xywd40dGTHz5o-5b_XleTUOcP2PyCenr6jw_uq_0xEsNEw5hg0t9xddwm7eqz0GeLTsFi9PQ3lgtjmbvLNnXInC3oMbs54hSuePsfPkc0Q1iSI482aydU1dSVqndwV1M7DoBed25dBROxKVeE0oQY0axIudZaYA4xwRQigtGMvCtsW9lzddYFtvuTPDdq39R1MbaHplEn2Tf94aCa2mh7rKU-ykN3PCjcoyqoVVLVspKnfV3Jer8zJ4v741kdTnKPp8aKWuKkye2cu067EIeCmBdsM4Vj4XSHjtdlqJRx2g9lIvtDKJWXY2xzUtktA4taOuLEbzCJksNWWwtveduGTOHnVC-sB9zE_3U_3m26YomuHVOaOc_IOgEDpXHpdiZMQl1y3e0o5xj-RJOEuqxcWKjLSuefAAAA__8GccXD">