[clang-tools-extra] [clang-tidy] Add performance-move-smart-pointer-contents check. (PR #66139)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 12 18:39:17 PDT 2023


https://github.com/HerrCai0907 requested changes to this pull request.

This check has some issue in my opinion, the semantic of `std::move(*p)` are not same as `*std::move(p)`

```c++
#include <iostream>
#include <memory>
#include <string>

using std::unique_ptr;

void f1() {
  unique_ptr<std::string> p{new std::string("demo")};
  std::cout << p.get() << " *p=" << *p << "\n";
  auto x = std::move(*p);
  std::cout << p.get() << " *p=" << *p << " x=" << x << "\n";
}

void f2() {
  unique_ptr<std::string> p{new std::string("demo")};
  std::cout << p.get() << " *p=" << *p << "\n";
  auto x = *std::move(p);
  std::cout << p.get() << " *p=" << *p << " x=" << x << "\n";
}

int main() {
  std::cout << "std::move(*p)\n";
  f1();
  std::cout << "*std::move(p)\n";
  f2();
}
```

The output is 
```
std::move(*p)
0x603000001b40 *p=demo
0x603000001b40 *p= x=demo
*std::move(p)
0x603000001b70 *p=demo
0x603000001b70 *p=demo x=demo
```

In `*std::move(p)`, move constructor does not happen, it is same as *p.

https://github.com/llvm/llvm-project/pull/66139


More information about the cfe-commits mailing list