[clang-tools-extra] [clang-tidy] fix false positive in lambda expr for return-const-ref-from-parameter (PR #118990)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 6 16:03:11 PST 2024


https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/118990

>From d9f50466850ee966829ce80f273ad9a10aa24489 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Fri, 6 Dec 2024 23:45:56 +0800
Subject: [PATCH] [clang-tidy] fix false positive in lambda expr for
 return-const-ref-from-parameter

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

diff --git a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
index a35fcd99d494af5..295955a971d7e87 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
@@ -31,22 +31,20 @@ void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
                              qualType(lValueReferenceType(pointee(
                                           qualType(isConstQualified()))))
                                  .bind("type"))),
-                         hasDeclContext(functionDecl().bind("owner")),
+                         hasDeclContext(functionDecl(
+                             equalsBoundNode("func"),
+                             hasReturnTypeLoc(loc(qualType(
+                                 hasCanonicalType(equalsBoundNode("type"))))))),
                          unless(hasLifetimeBoundAttr()))
                  .bind("param")))
           .bind("dref"));
-  const auto Func =
-      functionDecl(equalsBoundNode("owner"),
-                   hasReturnTypeLoc(loc(
-                       qualType(hasCanonicalType(equalsBoundNode("type"))))))
-          .bind("func");
 
   Finder->addMatcher(
       returnStmt(
+          hasAncestor(functionDecl().bind("func")),
           hasReturnValue(anyOf(
               DRef, ignoringParens(conditionalOperator(eachOf(
-                        hasTrueExpression(DRef), hasFalseExpression(DRef)))))),
-          hasAncestor(Func)),
+                        hasTrueExpression(DRef), hasFalseExpression(DRef))))))),
       this);
 }
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e00f86f7d01447f..b2b66dca6ccf853 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,8 +183,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`` and no longer giving
-  false positives for functions which contain lambda and ignore parameters
+  by using the conditional operator ``cond ? var1 : var2`` and fixing false
+  positives for functions which contain lambda and ignore parameters
   with ``[[clang::lifetimebound]]`` attribute.
   
 - Improved :doc:`bugprone-sizeof-expression
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 46cb9063beda976..a3297ca0f8084e9 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
@@ -203,3 +203,26 @@ namespace use_lifetime_bound_attr {
 int const &f(int const &a [[clang::lifetimebound]]) { return a; }
 } // namespace use_lifetime_bound_attr
 } // namespace gh117696
+
+
+namespace lambda {
+using T = const int &;
+using K = const float &;
+T inner_valid_lambda(T a) {
+  [&]() -> T { return a; };
+  return a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference parameter
+}
+T inner_invalid_lambda(T a) {
+  [&](T a) -> T { return a; };
+  // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: returning a constant reference parameter
+  return a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference parameter
+}
+T inner_invalid_lambda2(T a) {
+  [&](K a) -> K { return a; };
+  // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: returning a constant reference parameter
+  return a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference parameter
+}
+} // namespace lambda



More information about the cfe-commits mailing list