Issue 84324
Summary make iterator checks in `modernize-use-auto` optional
Labels new issue
Assignees
Reporter firewave
    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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to