<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/110599>110599</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang-tidy] modernize-use-ranges does not preserve constness
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-tidy
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          felix642
      </td>
    </tr>
</table>

<pre>
    The fix-it for modernize-use-ranges changes the constness on some function calls.
Let's assume the following example : 
```
void foo(int& a);
void foo(const int& a);

struct Foo
{
    std::vector<int> a;

    void print() {
       std::for_each(a.cbegin(), a.cend(), [](auto &x) {
        foo(x);
    });

    }
};
```
The method currently calls `void foo(const int& a)` since we are using `cbegin()` and `cend()`, but after applying the fix-it the code looks like this : 

```
void foo(int& a);
void foo(const int& a);

struct Foo
{
    std::vector<int> a;

    void print() {
 std::ranges::for_each(a, [](auto &x) {
        foo(x);
    });

 }
};
``` 
Which will call void foo(int &a) since we have removed the constness from the iterators.
To properly fix the issue the fix-it should add `as_const` around the range and include `<utiity>` if the user was previously using `cbegin()` and `cend()`.

godbolt: https://godbolt.org/z/MPnoo4vGY
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzclUuv4jYUxz-N2RyBgg0JWWRx5zJ000pdjFR1NXLsE-KO44P84MJ8-soOU7gPjdRFN5UQUezzyP9_follCOboEDu2_cS2-4VMcSTfDWjNpd7wRU_62n0ZEQZzWZoIA3mYSKN35jsuU8Cll-6IAdQ4X-OIoMiF6DAEIAeBJoQhORUNOVDS2rBi1Z5VT79iZLwJIENIE5bMgaylF-OOgBc5nSwCE08wh7O6uv3K7ZmMhoGI8Z1xkfEaJOMtE5_ebZfHgY-C5v8QfVIRDkS31ea2DQAQombiiYmnM6pInonnXEh8BvmmSg4uTU--tNox3sKrUo_VBvJfUaqR8Z1cqR6Pxs0pjD-DXCl0-n4_zyaHpkjAeH35qPZN7eVRYF5mzf695h8bN8H7-_Zrl_PoJ4wjaVDJe3TRXuchAqurn5pcVxCMUwgvCNIjpJDnyurqldy6Aul0Wb5rrqssu08R5BDRgzyd7DVnxzuJM2gawRJ9C2DNt4yQCY_E_A-4-afO_J69p-e_AuTndNwc_mM0aoQXY22BAl6bm58k-3bnYJRnBI8TnVG_-VQMnqayZCJ6Gcn_-Ex8ITh5OqG31zz7OSaEhI80hJGS1SB1QUmGr6VwoctTcnOzYmHBzThlk8Ycy8RzisbEKxOfc7wZSmwK6OFFBjh5PBtKwV7_HcGrRzePpHuyMaM5xngqY-QHxg-3jRX5I-OH74wffvvdEW3Ov_y50J3QrWjlArt1w5tG7ATni7Fr1uvdWqNosRJ13yMOg8aG79aiHdZKqIXpeMU3VSsqLta7DV_pdttU26Ha1f1m27SCbSqcpLEra89T7r0ohnbrdbVt24WVPdpQjgTOlZXuuIxGXxnn-YjwXc5a9ukY2KayJsRwrxNNtOUweUjb7j8-MjRhAEcxWxzQnx9oWCRvuzdOmTimfqVoYvyQ-90uy5Onv1BFxg9FRGD8cNNx7vjfAQAA__9X7QUt">