[clang-tools-extra] [clang-tidy][performance-move-const-arg] Fix crash when argument type has no definition (PR #111472)

Nicolas van Kempen via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 7 20:08:25 PDT 2024


https://github.com/nicovank created https://github.com/llvm/llvm-project/pull/111472


Fix #111450.

When the argument type is forward-declared and there is no definition, `hasMoveConstructor` triggers the assert here:
https://github.com/llvm-project/llvm-project/blob/7f65377880ce6a0e5eaa4cb2591b86b8c8a24ee6/clang/include/clang/AST/DeclCXX.h#L465
https://github.com/llvm/llvm-project/blob/7f65377880ce6a0e5eaa4cb2591b86b8c8a24ee6/clang/include/clang/AST/DeclCXX.h#L864

Check `hasDeclaration()` before `hasMoveConstructor()`.


>From f6e25e5ad0d2ddda7642afef8c6c0f5b8062d693 Mon Sep 17 00:00:00 2001
From: Nicolas van Kempen <nvankemp at gmail.com>
Date: Mon, 7 Oct 2024 23:06:45 -0400
Subject: [PATCH] [clang-tidy][performance-move-const-arg] Fix crash when
 argument type has no definition

Fix #111450.

When the argument type is forward-declared and there is no definition, `hasMoveConstructor` triggers the assert here:
https://github.com/llvm-project/llvm-project/blob/7f65377880ce6a0e5eaa4cb2591b86b8c8a24ee6/clang/include/clang/AST/DeclCXX.h#L465
https://github.com/llvm/llvm-project/blob/7f65377880ce6a0e5eaa4cb2591b86b8c8a24ee6/clang/include/clang/AST/DeclCXX.h#L864

Check `hasDeclaration()` before `hasMoveConstructor()`.
---
 .../clang-tidy/performance/MoveConstArgCheck.cpp   |  5 +++--
 .../checkers/performance/move-const-arg.cpp        | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
index d29b9e91f2e35d..421ce003975bc9 100644
--- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
@@ -209,8 +209,9 @@ void MoveConstArgCheck::check(const MatchFinder::MatchResult &Result) {
     }
 
     if (const CXXRecordDecl *RecordDecl = ArgType->getAsCXXRecordDecl();
-        RecordDecl && !(RecordDecl->hasMoveConstructor() &&
-                        RecordDecl->hasMoveAssignment())) {
+        RecordDecl && RecordDecl->hasDefinition() &&
+        !(RecordDecl->hasMoveConstructor() &&
+          RecordDecl->hasMoveAssignment())) {
       const bool MissingMoveAssignment = !RecordDecl->hasMoveAssignment();
       const bool MissingMoveConstructor = !RecordDecl->hasMoveConstructor();
       const bool MissingBoth = MissingMoveAssignment && MissingMoveConstructor;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp
index 4505eef6df24bd..8e325b0ae6ca30 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp
@@ -546,3 +546,17 @@ void testAlsoNonMoveable() {
 }
 
 } // namespace issue_62550
+
+namespace GH111450 {
+struct Status;
+
+struct Error {
+    Error(const Status& S);
+};
+
+struct Result {
+  Error E;
+  Result(Status&& S) : E(std::move(S)) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
+};
+} // namespace GH111450



More information about the cfe-commits mailing list