Skip to content

Commit eedcd9c

Browse files
committed
[clang-tidy misc-move-const-arg] more specific messages + suggest alternative solution
llvm-svn: 279666
1 parent 9c3633f commit eedcd9c

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

clang-tools-extra/clang-tidy/misc/MoveConstantArgumentCheck.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@ void MoveConstantArgumentCheck::check(const MatchFinder::MatchResult &Result) {
7474

7575
if (IsConstArg || IsTriviallyCopyable) {
7676
bool IsVariable = isa<DeclRefExpr>(Arg);
77+
const auto *Var =
78+
IsVariable ? dyn_cast<DeclRefExpr>(Arg)->getDecl() : nullptr;
7779
auto Diag = diag(FileMoveRange.getBegin(),
7880
"std::move of the %select{|const }0"
79-
"%select{expression|variable}1 "
80-
"%select{|of a trivially-copyable type }2"
81-
"has no effect; remove std::move()")
82-
<< IsConstArg << IsVariable << IsTriviallyCopyable;
81+
"%select{expression|variable %4}1 "
82+
"%select{|of the trivially-copyable type %5 }2"
83+
"has no effect; remove std::move()"
84+
"%select{| or make the variable non-const}3")
85+
<< IsConstArg << IsVariable << IsTriviallyCopyable
86+
<< (IsConstArg && IsVariable && !IsTriviallyCopyable)
87+
<< Var << Arg->getType();
8388

8489
ReplaceCallWithArg(CallMove, Diag, SM, getLangOpts());
8590
} else if (ReceivingExpr) {

clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,27 @@ class A {
2323

2424
int f1() {
2525
return std::move(42);
26-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the expression of a trivially-copyable type has no effect; remove std::move() [misc-move-const-arg]
26+
// 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]
2727
// CHECK-FIXES: return 42;
2828
}
2929

3030
int f2(int x2) {
3131
return std::move(x2);
32-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable of a trivially-copyable type
32+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'x2' of the trivially-copyable type 'int'
3333
// CHECK-FIXES: return x2;
3434
}
3535

3636
int *f3(int *x3) {
3737
return std::move(x3);
38-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable of a trivially-copyable type
38+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'x3' of the trivially-copyable type 'int *'
3939
// CHECK-FIXES: return x3;
4040
}
4141

4242
A f4(A x4) { return std::move(x4); }
4343

4444
A f5(const A x5) {
4545
return std::move(x5);
46-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable
46+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x5' has no effect; remove std::move() or make the variable non-const [misc-move-const-arg]
4747
// CHECK-FIXES: return x5;
4848
}
4949

@@ -55,7 +55,7 @@ void f7() { int a = f6(10); }
5555
void f8() {
5656
const A a;
5757
M1(A b = std::move(a);)
58-
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: std::move of the const variable
58+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: std::move of the const variable 'a' has no effect; remove std::move() or make the variable non-const
5959
// CHECK-FIXES: M1(A b = a;)
6060
}
6161

@@ -64,7 +64,7 @@ int f9() { return M2(1); }
6464

6565
template <typename T> T f10(const int x10) {
6666
return std::move(x10);
67-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable
67+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x10' of the trivially-copyable type 'const int' has no effect; remove std::move() [misc-move-const-arg]
6868
// CHECK-FIXES: return x10;
6969
}
7070
void f11() {

0 commit comments

Comments
 (0)