<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/124794>124794</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Misleading fixit from modernize-use-ranges for remove-erase
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
chrchr-github
</td>
</tr>
</table>
<pre>
~~~c++
void f(std::vector<int>& v) {
v.erase(std::remove(v.begin(), v.end(), 5), v.end());
}
void g(std::vector<int>& v) {
auto er = std::ranges::remove(v, 5);
v.erase(er.begin(), er.end());
}
~~~
~~~
<source>:5:13: warning: use a ranges version of this algorithm [modernize-use-ranges]](javascript:;)
1 | #include <vector>
2 | #include <algorithm>
3 |
4 | void f(std::vector<int>& v) {
5 | v.erase(std::remove(v.begin(), v.end(), 5), v.end());
| ^~~~~~~~~~~ ~~~~~~~~~ ~~~~~~~
| std::ranges::remove v
~~~
https://godbolt.org/z/fd9bzhezE
The naive approach `v.erase(std::remove(v.begin(), v.end(), 5), v.end());` or even `v.erase(std::remove(v.begin(), v.end(), 5));` doesn't compile, and this is not indicated by the fixit.
Also, the code that uses ranges is not shorter or more readable than the original, and requires an additional named variable.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0VMuupDYQ_ZpiU-oW2LiBBYu-j95llx8wuACPjN2xDZO5i_72yPQrubqJNFLGQmCbOlWnysclQ9CjJWpBvIB4y-QSJ-fbfvL95HejjtPSZZ1TP9rL5dIDe0lPflydVjgAq0NUwI_Ajyv10Xngr9pG4O_ADrgCaxCqZI_rnrwM9HeEp9mtaWfddzRqC6wG1gB7TcZWPZfiq-0GeHIM1Vt63xiNP8EIEeUSHZJH4G_4pCXtSOEzxQcP_ikd8p_Zk_9XnpfL5fEG_hrc4ntK3PhRAD8WHPgRv0tvtR3TdAmEEq-McCUftLPoBoyTDijN6LyO04wgXmanyFv9Qbsl0O6Wg3hLD6u_yVWG3utz3NJ6SayuFSgQqlcExrXtzaIIgb_e6_Z-s2Ff2DxiP8341ey6KLfFT6sEEcWGTONXSWZz_ohyHyDeL4-Bz9l9-g_gf4gF18cJTzGet3_sBOw0OtU5E_fOj8BOH8BOg2q6j4k-3q8K_n0itFKvhPJ89k72E8Ih_zVVOOToPNJK9v-JcXeqHAULrIrYu_msDSUTadVVsTqgdRG1VbqXkRR2PzBOhIP-U8c95MejCS4h0mbvFGGcZEy3INwvwc1FmJyP5FMSs_OEnqSSndkAdoM7r0dtpbkT8PTHoj0FlBalUjpqZ6VBK2dSuEqvE3yfqZarhjcyo7aoeJ3XJRNFNrVFMxwYr4vhoLisu15Q3ZWsqUQnqq7seKZbljORF6wuqiIXxZ7KRsiBiaE_yKIUPZQ5zVKbvTHrnFSQ6RAWagtWVk2ZGdmRCVsbZszSd9z-AmOpK_s2gXbdMgYoc6NDDE83UUdD7W86GJJK2_FaThy8m_GrtoCD83g939127tniTftJrFvj3_duBnZKkW6f3dm7b9RHYKeNXwB2uiWwtuyvAAAA__8wTOPX">