summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test
diff options
context:
space:
mode:
authorAaron Ballman <aaron@aaronballman.com>2017-11-27 22:59:33 +0000
committerAaron Ballman <aaron@aaronballman.com>2017-11-27 22:59:33 +0000
commitc566139632f4221b363931642eb637e0757a78d7 (patch)
tree700d0d74f4d9c82a7004cd7a2f51eecc14414fb0 /clang-tools-extra/test
parent256cc48df62c8dc394ee71ebec85b8b10eb1cddf (diff)
downloadbcm5719-llvm-c566139632f4221b363931642eb637e0757a78d7.tar.gz
bcm5719-llvm-c566139632f4221b363931642eb637e0757a78d7.zip
Add an option to misc-move-const-arg to not diagnose on trivially copyable types.
Patch by Oleg Smolsky llvm-svn: 319111
Diffstat (limited to 'clang-tools-extra/test')
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-move-const-arg-trivially-copyable.cpp98
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp56
2 files changed, 154 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/misc-move-const-arg-trivially-copyable.cpp b/clang-tools-extra/test/clang-tidy/misc-move-const-arg-trivially-copyable.cpp
new file mode 100644
index 00000000000..57602e17e49
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/misc-move-const-arg-trivially-copyable.cpp
@@ -0,0 +1,98 @@
+// RUN: %check_clang_tidy %s misc-move-const-arg %t \
+// RUN: -config='{CheckOptions: \
+// RUN: [{key: misc-move-const-arg.CheckTriviallyCopyableMove, value: 0}]}' \
+// RUN: -- -std=c++14
+
+namespace std {
+
+template <typename> struct remove_reference;
+template <typename _Tp> struct remove_reference { typedef _Tp type; };
+template <typename _Tp> struct remove_reference<_Tp &> { typedef _Tp type; };
+template <typename _Tp> struct remove_reference<_Tp &&> { typedef _Tp type; };
+
+template <typename _Tp>
+constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) {
+ return static_cast<typename std::remove_reference<_Tp>::type &&>(__t);
+}
+
+template <typename _Tp>
+constexpr _Tp &&
+forward(typename remove_reference<_Tp>::type &__t) noexcept {
+ return static_cast<_Tp &&>(__t);
+}
+
+} // namespace std
+
+class NoMoveSemantics {
+ public:
+ NoMoveSemantics();
+ NoMoveSemantics(const NoMoveSemantics &);
+
+ NoMoveSemantics &operator=(const NoMoveSemantics &);
+};
+
+void callByConstRef(const NoMoveSemantics &);
+void callByConstRef(int i, const NoMoveSemantics &);
+
+void moveToConstReferencePositives() {
+ NoMoveSemantics obj;
+
+ // Basic case. It is here just to have a single "detected and fixed" case.
+ callByConstRef(std::move(obj));
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as a const reference argument; no move will actually happen [misc-move-const-arg]
+ // CHECK-FIXES: callByConstRef(obj);
+}
+
+struct TriviallyCopyable {
+ int i;
+};
+
+void f(TriviallyCopyable) {}
+
+void g() {
+ TriviallyCopyable obj;
+ f(std::move(obj));
+}
+
+class MoveSemantics {
+ public:
+ MoveSemantics();
+ MoveSemantics(MoveSemantics &&);
+
+ MoveSemantics &operator=(MoveSemantics &&);
+};
+
+void fmovable(MoveSemantics);
+
+void lambda1() {
+ auto f = [](MoveSemantics m) {
+ fmovable(std::move(m));
+ };
+ f(MoveSemantics());
+}
+
+template<class T> struct function {};
+
+template<typename Result, typename... Args>
+class function<Result(Args...)> {
+public:
+ function() = default;
+ void operator()(Args... args) const {
+ fmovable(std::forward<Args>(args)...);
+ }
+};
+
+void functionInvocation() {
+ function<void(MoveSemantics)> callback;
+ MoveSemantics m;
+ callback(std::move(m));
+}
+
+void lambda2() {
+ function<void(MoveSemantics)> callback;
+
+ auto f = [callback = std::move(callback)](MoveSemantics m) mutable {
+ callback(std::move(m));
+ };
+ f(MoveSemantics());
+}
diff --git a/clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp
index bbe6c6bd865..f4b28066a3a 100644
--- a/clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp
+++ b/clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp
@@ -14,6 +14,12 @@ constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) {
return static_cast<typename std::remove_reference<_Tp>::type &&>(__t);
}
+template <typename _Tp>
+constexpr _Tp &&
+forward(typename remove_reference<_Tp>::type &__t) noexcept {
+ return static_cast<_Tp &&>(__t);
+}
+
} // namespace std
class A {
@@ -23,6 +29,19 @@ public:
A(A &&rhs) {}
};
+struct TriviallyCopyable {
+ int i;
+};
+
+void f(TriviallyCopyable) {}
+
+void g() {
+ TriviallyCopyable obj;
+ f(std::move(obj));
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: std::move of the variable 'obj' of the trivially-copyable type 'TriviallyCopyable' has no effect; remove std::move() [misc-move-const-arg]
+ // CHECK-FIXES: f(obj);
+}
+
int f1() {
return std::move(42);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the expression of the trivially-copyable type 'int' has no effect; remove std::move() [misc-move-const-arg]
@@ -176,3 +195,40 @@ void Q(T);
void moveOnlyNegatives(MoveOnly val) {
Q(std::move(val));
}
+
+void fmovable(MoveSemantics);
+
+void lambda1() {
+ auto f = [](MoveSemantics m) {
+ fmovable(std::move(m));
+ };
+ f(MoveSemantics());
+}
+
+template<class T> struct function {};
+
+template<typename Result, typename... Args>
+class function<Result(Args...)> {
+public:
+ function() = default;
+ void operator()(Args... args) const {
+ fmovable(std::forward<Args>(args)...);
+ }
+};
+
+void functionInvocation() {
+ function<void(MoveSemantics)> callback;
+ MoveSemantics m;
+ callback(std::move(m));
+}
+
+void lambda2() {
+ function<void(MoveSemantics)> callback;
+
+ auto f = [callback = std::move(callback)](MoveSemantics m) mutable {
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: std::move of the variable 'callback' of the trivially-copyable type 'function<void (MoveSemantics)>' has no effect; remove std::move()
+ // CHECK-FIXES: auto f = [callback = callback](MoveSemantics m) mutable {
+ callback(std::move(m));
+ };
+ f(MoveSemantics());
+}
OpenPOWER on IntegriCloud