<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/133110>133110</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-tidy] Check request: modernize-use-std-exchange
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
denzor200
</td>
</tr>
</table>
<pre>
Needs a check that will find places in the code with potential for using `std::exchange` and will suggest using it.
At least this feature should be implemented for checking move-constructors and move-assignments.
BEFORE:
```
struct S
{
int n;
S(S&& other) noexcept {
n = other.n;
other.n = 0;
}
S& operator=(S&& other) noexcept {
if (this != &other) {
n = other.n;
other.n = 0;
}
return *this;
}
};
```
AFTER:
```
struct S
{
int n;
S(S&& other) noexcept {
n = std::exchange(other.n, 0);
}
S& operator=(S&& other) noexcept {
n = std::exchange(other.n, 0);
return *this;
}
};
```
Also I suppose it's possible to implement checking of regular functions.
BEFORE:
```
class stream
{
public:
int flags(int newf) {
const int old_flags = flags_;
flags_ = newf;
return old_flags;
}
private:
int flags_ = 0;
};
```
AFTER:
```
class stream
{
public:
int flags(int newf) { return std::exchange(flags_, newf); }
private:
int flags_ = 0;
};
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8VVGP2zYM_jXKC5FAkRP78uAH3-UC7GUDensvZIu2tSmSJ9J3bX_9YNm5pOutxQ1DhQCxKJLfx48UpIls5xFLsb8X--NKj9yHWBr0X0JUUq7qYD6XQla_IhoCDU2PzZ_AvWZ4sc5Ba72BwekGCawH7hGaYBBeLPcwBEbPVjtoQ4SRrO9A5JLYiKwSWYWfml77DkUuQXszZ6Sx65B4cbe8EbISsqoYHGpi4N4StKh5jAjUh9EZqBHseXB4Rs9oEloiOmU4h2dcN8ETx7HhEClBJetc_RREC8r94-m3D48TO1mJXC4_Wc3B8DSZi3shKwAA6xm8yNJ2MT0JdfckVC5UDoF7jEIdwAf81ODAcA2dlgeRHWevzSXNshZjcpDXI1EcZ5oTTg5hwKg5RJEd3wFrWxDqLqko1HZCECp_DVp8v8PtO_yuHC-7iDxGD0JVE-BFq7mM4jjvb3WeGn36_fHDz-zAt-Oo7i6VqweQQh3easGM9V-78F7oN4W88vk3LR0F-AVoHIZACJaFKgiGQGRrh8Dhem2uFya0ELEbnY7Qjr5hG_yPrkfjNBEQR9Tn1wYNY-1sMzunTrVOdyTUXeoavrQ387asdE2Tb3DmY_JPOqWvj18P2mxLxynZV4eLXK9Z_inYEO2zZlzILZN0k3EZ6nfO6I9luEX6VokL7bfGYpFAPVwCRHb_PxSzMmVmDtlBr7DcFrtMyuywV6u-3O2Kwsg7RI2qxexQ5KY-mGInsTayRbWypZJqLzOVb_e7_U5u9vs2M1utD0WWa6mM2Ek8a-s2zj2fNyF2K0s0YrnNsu1Wrpyu0VF6eJRqnPbdmq35LJSaHqJYTlHreuxI7KSzxHTNw5ZderJuwvZHeEhvU8S_RiQWWQXnYDB6-wXXI-Ga2Kwvgq7G6MqeeaBJNXUS6tRZ7sd604SzUKcJavlbDzH8gQ0LdUr8SajTUsJzqf4OAAD__012Fa4">