[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:06:37 PST 2024


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

>From 5d35398c45631a58e283899419f00e4a5e0ba722 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   | 13 ++++++++-----
 clang-tools-extra/docs/ReleaseNotes.rst             |  3 ++-
 .../bugprone/return-const-ref-from-parameter.cpp    |  6 ++++++
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
index 7cc4fe519d3a64..64c66ee4ae1306 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
@@ -18,14 +18,17 @@ namespace clang::tidy::bugprone {
 void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
   const auto DRef = ignoringParens(
       declRefExpr(
-          to(parmVarDecl(hasType(hasCanonicalType(
-                             qualType(lValueReferenceType(pointee(
-                                          qualType(isConstQualified()))))
-                                 .bind("type"))))
+          to(parmVarDecl(
+                 hasType(hasCanonicalType(
+                     qualType(lValueReferenceType(
+                                  pointee(qualType(isConstQualified()))))
+                         .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