[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 08:26:27 PST 2024


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

Fix https://github.com/llvm/llvm-project/issues/115743

>From 37a518b35b205e3e2e7bfbeab64d2a334519c30d Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744285 at qq.com>
Date: Wed, 27 Nov 2024 00:22:05 +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                     | 4 ++++
 .../checkers/bugprone/return-const-ref-from-parameter.cpp   | 6 ++++++
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
index adb26ade955c5e..874f35f055094e 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
@@ -21,10 +21,10 @@ 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")))),
           hasAncestor(
-              functionDecl(hasReturnTypeLoc(loc(qualType(
+              functionDecl(equalsBoundNode("owner"), hasReturnTypeLoc(loc(qualType(
                                hasCanonicalType(equalsBoundNode("type"))))))
                   .bind("func")))
           .bind("ret"),
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index b72d109b3d3938..9c3a5e4f40f930 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -104,6 +104,10 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Improved :doc:`bugprone-return-const-ref-from-parameter
+  <clang-tidy/checks/bugprone/return-const-ref-from-parameter>` check by no longer
+  giving false positives for lambda.
+
 - Improved :doc:`readability-redundant-smartptr-get
   <clang-tidy/checks/readability/redundant-smartptr-get>` check to
   remove `->`, when reduntant `get()` is removed.
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 d13c127da7c2a1..391e8f96401ab4 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
@@ -142,6 +142,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