[clang-tools-extra] [clang-tidy] Fix assert in performance-unnecessary-copy-init. (PR #96506)

Clement Courbet via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 24 22:53:20 PDT 2024


https://github.com/legrosbuffle updated https://github.com/llvm/llvm-project/pull/96506

>From 834b63adeea098ec2d815a85fe47e7577ff3baa9 Mon Sep 17 00:00:00 2001
From: Clement Courbet <courbet at google.com>
Date: Mon, 24 Jun 2024 15:05:48 +0000
Subject: [PATCH] [clang-tidy] Fix assert in performance-unnecessary-copy-init.

`GetDirectCallee` can be null.

Fixes #96498.
---
 clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp  | 2 +-
 clang-tools-extra/docs/ReleaseNotes.rst                  | 2 +-
 .../performance/unnecessary-copy-initialization.cpp      | 9 +++++++++
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
index a6062ccf42230..106feb7fb4172 100644
--- a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
+++ b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
@@ -296,7 +296,7 @@ AST_MATCHER_P(DeclRefExpr, doesNotMutateObject, int, Indirections) {
           return false;
         }
         const auto *const Method =
-            dyn_cast<CXXMethodDecl>(OpCall->getDirectCallee());
+            dyn_cast_or_null<CXXMethodDecl>(OpCall->getDirectCallee());
 
         if (Method == nullptr) {
           // This is not a member operator. Typically, a friend operator. These
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 16fdc20eafd62..7fcf5cfbe3783 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -398,7 +398,7 @@ Changes in existing checks
   analyzed, so the check now handles the common patterns
   `const auto e = (*vector_ptr)[i]` and `const auto e = vector_ptr->at(i);`.
   Calls to mutable function where there exists a `const` overload are also
-  handled.
+  handled. Fix crash in the case of a non-member operator call.
 
 - Improved :doc:`readability-avoid-return-with-void-value
   <clang-tidy/checks/readability/avoid-return-with-void-value>` check by adding
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp
index f259552dc8f1d..d02bb98cf583c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp
@@ -906,3 +906,12 @@ void negativeNonConstMemberExpr() {
   }
 }
 
+
+bool operator==(ExpensiveToCopyType, ExpensiveToCopyType);
+
+template<typename T> bool OperatorWithNoDirectCallee(T t) {
+  ExpensiveToCopyType a1;
+  ExpensiveToCopyType a2 = a1;
+  return a1 == t;
+}
+



More information about the cfe-commits mailing list