[clang-tools-extra] [clang-tidy] Fix smart pointers handling in bugprone-use-after-move (PR #94869)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 12 07:13:54 PDT 2024
================
@@ -0,0 +1,59 @@
+// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-use-after-move %t -- \
+// RUN: -config="{CheckOptions: {bugprone-use-after-move.AllowMovedSmartPtrUse: false}}" -- -fno-delayed-template-parsing -I %S/../modernize/Inputs/smart-ptr/
+
+#include "unique_ptr.h"
+
+namespace PR90174 {
+
+struct A {};
+
+struct SinkA {
+ SinkA(std::unique_ptr<A>);
+};
+
+class ClassB {
+ ClassB(std::unique_ptr<A> aaa) : aa(std::move(aaa)) {
+ a = std::make_unique<SinkA>(std::move(aaa));
+ // CHECK-MESSAGES: [[@LINE-1]]:43: warning: 'aaa' used after it was moved
+ // CHECK-MESSAGES: [[@LINE-3]]:36: note: move occurred here
+ }
+ std::unique_ptr<A> aa;
+ std::unique_ptr<SinkA> a;
+};
+
+void s(const std::unique_ptr<A> &);
+
+template <typename T, typename... Args> auto my_make_unique(Args &&...args) {
+ return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}
+
+void natively(std::unique_ptr<A> x) {
+ std::unique_ptr<A> tmp = std::move(x);
+ std::unique_ptr<SinkA> y2{new SinkA(std::move(x))};
----------------
martinboehme wrote:
Is there a reason (here and possibly also below) that `SinkA` needs to be referenced via a `unique_ptr` instead of simply making it a local variable of `SinkA`?
I.e. why not simply do
```cxx
SinkA sink(std::move(x));
```
since the behavior we're interested in is what happens when we move `x` into the argument for the `SinkA` constructor, and not what happens with the `SinkA` itself.
(Also nit: I'm not clear on the reason for the `y2` variable name -- a) there is no `y1`, and b) maybe just `sink` to emphasize the type?)
https://github.com/llvm/llvm-project/pull/94869
More information about the cfe-commits
mailing list