<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/84324>84324</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
make iterator checks in `modernize-use-auto` optional
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
firewave
</td>
</tr>
</table>
<pre>
The suggestions of this check might undermine the constness of iterators as a type explicitly specified as `const_iterator` might result in `iterator` instead if the container is not `const`.
```cpp
#include <map>
std::map<int, int> cb();
void f()
{
std::map<int, int> m = cb();
std::map<int, int>::const_iterator I = m.begin();
}
```
```
<source>:8:5: warning: use auto when declaring iterators [modernize-use-auto]
8 | std::map<int, int>::const_iterator I = m.begin();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| auto
```
https://godbolt.org/z/7Gnr9d6ad
In the example it was not possible to modify the container if you add subsequent code. But if you introduce `auto` it is made possible.
This can be partially mitigated starting with C++11 by using `cbegin()` and `cend()`. (This seems like a candidate for a check in itself). `misc-const-correctness` might also be helpful in some cases.
Unfortunately there is no `cfind()` for containers like `std::map` so to mitigate those cases you have to use `std::as_const()` which is not available until C++17. (Also a candidate for a separate check).
So it would be helpful if you could disable the iterator part of this check. Or improve the check to only suggest cases where the resulting type behind `auto` might not be change/ambiguous.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU2T2jgT_jXi0jWUkcGYAwdgwls5vYfNnlOy1LZ7I0tefUDIYX_7lmQPgUkqqaUojLvVH3r66W7hPXUGcc82R7Z5XYgYeuv2LTm8igsuGqtu-089go9dhz6QNR5sC6EnD7JH-QUG6voA0Sh0AxmE0CNIa3ww6PNZCuhEsM6D8CAg3EYE_DpqkhT0DfyIklpCldSsKrLt5zcjVhVzBIc-6gBk0qFHNRkfUCig9i12EGTQAXkwNtx9sqpYsuKVFYf5tyqmrxzHWcJLMlJHhcDK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