Skip to content

Commit 5bb6af6

Browse files
committed
FixIt
1 parent 5359c69 commit 5bb6af6

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

clang-tools-extra/clang-tidy/performance/LostStdMoveCheck.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "LostStdMoveCheck.h"
1010
#include "../utils/DeclRefExprUtils.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/Lex/Lexer.h"
1213

1314
using namespace clang::ast_matchers;
1415

@@ -111,7 +112,14 @@ void LostStdMoveCheck::check(const MatchFinder::MatchResult& Result) {
111112
return;
112113
}
113114

114-
diag(LastUsage->getBeginLoc(), "could be std::move()");
115+
116+
const SourceManager &Source = Result.Context->getSourceManager();
117+
const auto Range = CharSourceRange::getTokenRange(LastUsage->getSourceRange());
118+
const StringRef NeedleExprCode = Lexer::getSourceText(
119+
Range, Source,
120+
Result.Context->getLangOpts());
121+
diag(LastUsage->getBeginLoc(), "could be std::move()")
122+
<< FixItHint::CreateReplacement(Range, ("std::move(" + NeedleExprCode + ")").str());
115123
}
116124

117125
} // namespace clang::tidy::performance

clang-tools-extra/test/clang-tidy/checkers/performance/lost-std-move.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,30 @@ void f_arg(std::shared_ptr<int> ptr)
2323
{
2424
if (*ptr)
2525
f(ptr);
26-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: Could be std::move() [performance-lost-std-move]
26+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: could be std::move() [performance-lost-std-move]
2727
}
2828

2929
void f_rvalue_ref(std::shared_ptr<int>&& ptr)
3030
{
3131
if (*ptr)
3232
f(ptr);
33-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: Could be std::move() [performance-lost-std-move]
33+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: could be std::move() [performance-lost-std-move]
3434
}
3535

3636
using SharedPtr = std::shared_ptr<int>;
3737
void f_using(SharedPtr ptr)
3838
{
3939
if (*ptr)
4040
f(ptr);
41-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: Could be std::move() [performance-lost-std-move]
41+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: could be std::move() [performance-lost-std-move]
4242
}
4343

4444
void f_local()
4545
{
4646
std::shared_ptr<int> ptr;
4747
if (*ptr)
4848
f(ptr);
49-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: Could be std::move() [performance-lost-std-move]
49+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: could be std::move() [performance-lost-std-move]
5050
}
5151

5252
void f_move()

0 commit comments

Comments
 (0)