[clang-tools-extra] [clang-tidy] fix false positive in bugprone-return-const-ref-from-parameter (PR #117734)

Qizhi Hu via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 26 09:04:34 PST 2024


https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/117734

>From 7dd899ec58a61b02a2bd584d021dcddfb20ceaba Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744285 at qq.com>
Date: Wed, 27 Nov 2024 00:57:00 +0800
Subject: [PATCH] [clang-tidy] fix false positive in
 bugprone-return-const-ref-from-parameter

---
 .../bugprone/ReturnConstRefFromParameterCheck.cpp           | 4 ++--
 clang-tools-extra/docs/ReleaseNotes.rst                     | 3 ++-
 .../checkers/bugprone/return-const-ref-from-parameter.cpp   | 6 ++++++
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
index 7cc4fe519d3a64..80d1487ba5d6fb 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
@@ -21,11 +21,11 @@ void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
           to(parmVarDecl(hasType(hasCanonicalType(
                              qualType(lValueReferenceType(pointee(
                                           qualType(isConstQualified()))))
-                                 .bind("type"))))
+                                 .bind("type"))), parmVarDecl(hasDeclContext(functionDecl().bind("owner"))))
                  .bind("param")))
           .bind("dref"));
   const auto Func =
-      functionDecl(hasReturnTypeLoc(loc(
+      functionDecl(equalsBoundNode("owner"), hasReturnTypeLoc(loc(
                        qualType(hasCanonicalType(equalsBoundNode("type"))))))
           .bind("func");
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index f8507156aa4198..4d3cfa00fd2766 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -179,7 +179,8 @@ Changes in existing checks
 - Improved :doc:`bugprone-return-const-ref-from-parameter
   <clang-tidy/checks/bugprone/return-const-ref-from-parameter>` check to
   diagnose potential dangling references when returning a ``const &`` parameter
-  by using the conditional operator ``cond ? var1 : var2``.
+  by using the conditional operator ``cond ? var1 : var2`` and no longer giving
+  false positives for lambda.
   
 - Improved :doc:`bugprone-sizeof-expression
   <clang-tidy/checks/bugprone/sizeof-expression>` check to find suspicious
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
index d83d997a455d50..5179a80f4d5b4b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
@@ -151,6 +151,12 @@ void instantiate(const int &param, const float &paramf, int &mut_param, float &m
         itf6(mut_paramf);
 }
 
+template<class T>
+void f(const T& t) {
+    const auto get = [&t] -> const T& { return t; };
+    return T{};
+}
+
 } // namespace valid
 
 namespace overload {



More information about the cfe-commits mailing list