[clang-tools-extra] 13c2934 - [clang-tidy] Add invalidation function name to bugprone-use-after-move (#178042)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 27 01:52:49 PST 2026
Author: Baranov Victor
Date: 2026-01-27T12:52:44+03:00
New Revision: 13c293486603d9c2e03888866a60df7d42f8e068
URL: https://github.com/llvm/llvm-project/commit/13c293486603d9c2e03888866a60df7d42f8e068
DIFF: https://github.com/llvm/llvm-project/commit/13c293486603d9c2e03888866a60df7d42f8e068.diff
LOG: [clang-tidy] Add invalidation function name to bugprone-use-after-move (#178042)
Make clearer messages because of reports
https://github.com/llvm/llvm-project/pull/170346#issuecomment-3798583117.
---------
Co-authored-by: EugeneZelenko <eugene.zelenko at gmail.com>
Added:
Modified:
clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
index a31dd1e5dbdcd..7dc648f8ed405 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -436,13 +436,15 @@ static MoveType determineMoveType(const FunctionDecl *FuncDecl) {
static void emitDiagnostic(const Expr *MovingCall, const DeclRefExpr *MoveArg,
const UseAfterMove &Use, ClangTidyCheck *Check,
- ASTContext *Context, MoveType Type) {
+ ASTContext *Context, MoveType Type,
+ const FunctionDecl *MoveDecl) {
const SourceLocation UseLoc = Use.DeclRef->getExprLoc();
const SourceLocation MoveLoc = MovingCall->getExprLoc();
- Check->diag(UseLoc,
- "'%0' used after it was %select{forwarded|moved|invalidated}1")
- << MoveArg->getDecl()->getName() << Type;
+ Check->diag(
+ UseLoc,
+ "'%0' used after it was %select{forwarded|moved|invalidated by %2}1")
+ << MoveArg->getDecl()->getName() << Type << MoveDecl;
Check->diag(MoveLoc, "%select{forward|move|invalidation}0 occurred here",
DiagnosticIDs::Note)
<< Type;
@@ -573,7 +575,7 @@ void UseAfterMoveCheck::check(const MatchFinder::MatchResult &Result) {
ReinitializationFunctions);
if (auto Use = Finder.find(CodeBlock, MovingCall, Arg))
emitDiagnostic(MovingCall, Arg, *Use, this, Result.Context,
- determineMoveType(MoveDecl));
+ determineMoveType(MoveDecl), MoveDecl);
}
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7f55341e2995b..1a056890e66c3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -131,6 +131,11 @@ Changes in existing checks
``std::get_temporary_buffer`` to the default list of unsafe functions. (This
function is unsafe, useless, deprecated in C++17 and removed in C++20).
+- Improved :doc:`bugprone-use-after-move
+ <clang-tidy/checks/bugprone/use-after-move>` check by including the name of
+ the invalidating function in the warning message when a custom invalidation
+ function is used (via the `InvalidationFunctions` option).
+
- Improved :doc:`llvm-use-ranges
<clang-tidy/checks/llvm/use-ranges>` check by adding support for the following
algorithms: ``std::accumulate``, ``std::replace_copy``, and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
index da818a90514f6..0f1678cff35a7 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
@@ -1,12 +1,12 @@
// RUN: %check_clang_tidy -std=c++11 -check-suffixes=,CXX11 %s bugprone-use-after-move %t -- \
// RUN: -config='{CheckOptions: { \
-// RUN: bugprone-use-after-move.InvalidationFunctions: "::Database<>::StaticCloseConnection;Database<>::CloseConnection;FriendCloseConnection", \
+// RUN: bugprone-use-after-move.InvalidationFunctions: "::Database<>::StaticCloseConnection;Database<>::CloseConnection;FriendCloseConnection;FreeCloseConnection", \
// RUN: bugprone-use-after-move.ReinitializationFunctions: "::Database<>::Reset;::Database<>::StaticReset;::FriendReset;::RegularReset" \
// RUN: }}' -- \
// RUN: -fno-delayed-template-parsing
// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-use-after-move %t -- \
// RUN: -config='{CheckOptions: { \
-// RUN: bugprone-use-after-move.InvalidationFunctions: "::Database<>::StaticCloseConnection;Database<>::CloseConnection;FriendCloseConnection", \
+// RUN: bugprone-use-after-move.InvalidationFunctions: "::Database<>::StaticCloseConnection;Database<>::CloseConnection;FriendCloseConnection;FreeCloseConnection", \
// RUN: bugprone-use-after-move.ReinitializationFunctions: "::Database<>::Reset;::Database<>::StaticReset;::FriendReset;::RegularReset" \
// RUN: }}' -- \
// RUN: -fno-delayed-template-parsing
@@ -1670,37 +1670,45 @@ struct Database {
void Query();
};
+void FreeCloseConnection(Database<int>&) {}
+
void Run() {
using DB = Database<>;
DB db1;
db1.CloseConnection();
db1.Query();
- // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db1' used after it was invalidated
+ // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db1' used after it was invalidated by 'CloseConnection<>'
// CHECK-NOTES: [[@LINE-3]]:7: note: invalidation occurred here
DB db2;
DB::StaticCloseConnection(db2);
db2.Query();
- // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db2' used after it was invalidated
+ // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db2' used after it was invalidated by 'StaticCloseConnection<>'
// CHECK-NOTES: [[@LINE-3]]:3: note: invalidation occurred here
DB db3;
DB().StaticCloseConnection(db3);
db3.Query();
- // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db3' used after it was invalidated
+ // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db3' used after it was invalidated by 'StaticCloseConnection<>'
// CHECK-NOTES: [[@LINE-3]]:3: note: invalidation occurred here
DB db4;
FriendCloseConnection(db4);
db4.Query();
- // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db4' used after it was invalidated
+ // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db4' used after it was invalidated by 'FriendCloseConnection<>'
// CHECK-NOTES: [[@LINE-3]]:3: note: invalidation occurred here
DB db5;
FriendCloseConnection(db5, /*disconnect timeout*/ 5);
db5.Query();
- // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db5' used after it was invalidated
+ // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db5' used after it was invalidated by 'FriendCloseConnection<>'
+ // CHECK-NOTES: [[@LINE-3]]:3: note: invalidation occurred here
+
+ DB db6;
+ FreeCloseConnection(db6);
+ db6.Query();
+ // CHECK-NOTES: [[@LINE-1]]:3: warning: 'db6' used after it was invalidated by 'FreeCloseConnection'
// CHECK-NOTES: [[@LINE-3]]:3: note: invalidation occurred here
}
More information about the cfe-commits
mailing list