Skip to content

Commit

Permalink
[clang-tidy] Fix check for trivially copyable types in modernize-pass…
Browse files Browse the repository at this point in the history
…-by-value

Summary:
rL270567 excluded trivially copyable types from being moved by
modernize-pass-by-value, but it didn't exclude references to them.
Change types used in the tests to not be trivially copyable.

Reviewers: madsravn, aaron.ballman, alexfh

Subscribers: JDevlieghere, cfe-commits

Differential Revision: https://reviews.llvm.org/D28614

llvm-svn: 291796
  • Loading branch information
pepsiman committed Jan 12, 2017
1 parent bec58f9 commit 6b3e272
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ void PassByValueCheck::check(const MatchFinder::MatchResult &Result) {

// If the parameter is trivial to copy, don't move it. Moving a trivivally
// copyable type will cause a problem with misc-move-const-arg
if (ParamDecl->getType().isTriviallyCopyableType(*Result.Context))
if (ParamDecl->getType().getNonReferenceType().isTriviallyCopyableType(
*Result.Context))
return;

auto Diag = diag(ParamDecl->getLocStart(), "pass by value and use std::move");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class ThreadId {
public:
ThreadId(const ThreadId &) {}
ThreadId(ThreadId &&) {}
};

struct A {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// RUN: FileCheck -input-file=%T/pass-by-value-header.h %s -check-prefix=CHECK-FIXES

#include "pass-by-value-header.h"
// CHECK-MESSAGES: :5:5: warning: pass by value and use std::move [modernize-pass-by-value]
// CHECK-MESSAGES: :8:5: warning: pass by value and use std::move [modernize-pass-by-value]
// CHECK-FIXES: #include <utility>
// CHECK-FIXES: A(ThreadId tid) : threadid(std::move(tid)) {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include HEADER

struct A {
A(const A &) {}
A(A &&) {}
};

struct B {
Expand Down
18 changes: 16 additions & 2 deletions clang-tools-extra/test/clang-tidy/modernize-pass-by-value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace {
// POD types are trivially move constructible.
struct POD {
int a, b, c;
};

struct Movable {
int a, b, c;
Movable() = default;
Movable(const Movable &) {}
Movable(Movable &&) {}
};

struct NotMovable {
Expand Down Expand Up @@ -147,7 +154,8 @@ template <typename T> struct N {
// Test with value parameter.
struct O {
O(Movable M) : M(M) {}
// CHECK-FIXES: O(Movable M) : M(M) {}
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move
// CHECK-FIXES: O(Movable M) : M(std::move(M)) {}
Movable M;
};

Expand Down Expand Up @@ -179,7 +187,8 @@ typedef ::Movable RMovable;
}
struct R {
R(ns_R::RMovable M) : M(M) {}
// CHECK-FIXES: R(ns_R::RMovable M) : M(M) {}
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move
// CHECK-FIXES: R(ns_R::RMovable M) : M(std::move(M)) {}
ns_R::RMovable M;
};

Expand All @@ -199,3 +208,8 @@ struct T {
// CHECK-FIXES: T(array<int, 10> a) : a_(a) {}
array<int, 10> a_;
};

struct U {
U(const POD &M) : M(M) {}
POD M;
};

0 comments on commit 6b3e272

Please sign in to comment.