[clang-tools-extra] [clang-tidy] Let `bugprone-use-after-move` also handle calls to `std::forward` (PR #82673)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 23 06:59:36 PST 2024


================
@@ -359,24 +360,52 @@ void UseAfterMoveFinder::getReinits(
   }
 }
 
+enum class MoveType {
+  Move,    // std::move
+  Forward, // std::forward
+};
+
+static MoveType determineMoveType(const FunctionDecl *FuncDecl) {
+  if (FuncDecl->getName() == "move")
+    return MoveType::Move;
+  if (FuncDecl->getName() == "forward")
+    return MoveType::Forward;
+
+  assert(false && "Invalid move type");
+}
+
 static void emitDiagnostic(const Expr *MovingCall, const DeclRefExpr *MoveArg,
                            const UseAfterMove &Use, ClangTidyCheck *Check,
-                           ASTContext *Context) {
+                           ASTContext *Context, MoveType Type) {
   SourceLocation UseLoc = Use.DeclRef->getExprLoc();
   SourceLocation MoveLoc = MovingCall->getExprLoc();
 
-  Check->diag(UseLoc, "'%0' used after it was moved")
-      << MoveArg->getDecl()->getName();
-  Check->diag(MoveLoc, "move occurred here", DiagnosticIDs::Note);
+  StringRef ActionType;
+  StringRef ActionTypePastTense;
+  switch (Type) {
+  case MoveType::Move:
+    ActionType = "move";
+    ActionTypePastTense = "moved";
+    break;
+  case MoveType::Forward:
+    ActionType = "forward";
+    ActionTypePastTense = "forwarded";
+    break;
+  }
+
+  Check->diag(UseLoc, "'%0' used after it was %1")
+      << MoveArg->getDecl()->getName() << ActionTypePastTense;
+  Check->diag(MoveLoc, "%0 occurred here", DiagnosticIDs::Note) << ActionType;
   if (Use.EvaluationOrderUndefined) {
     Check->diag(UseLoc,
-                "the use and move are unsequenced, i.e. there is no guarantee "
+                "the use and %0 are unsequenced, i.e. there is no guarantee "
                 "about the order in which they are evaluated",
-                DiagnosticIDs::Note);
+                DiagnosticIDs::Note)
+        << ActionType;
   } else if (UseLoc < MoveLoc || Use.DeclRef == MoveArg) {
-    Check->diag(UseLoc,
-                "the use happens in a later loop iteration than the move",
-                DiagnosticIDs::Note);
+    Check->diag(UseLoc, "the use happens in a later loop iteration than the %0",
+                DiagnosticIDs::Note)
+        << ActionType;
----------------
5chmidti wrote:

You could use the diagnostics 'select' syntax to do this in a more compact way: https://github.com/llvm/llvm-project/blob/be083dba95dfbbb0286d798cc06fbe021715bc03/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp#L96-L98

https://github.com/llvm/llvm-project/pull/82673


More information about the cfe-commits mailing list