[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
Thu Nov 28 06:39:24 PST 2024
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/117734
>From 7100846f3d04fc973fcf4535b0a8572632228ead 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 | 11 +++++++++++
3 files changed, 21 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..4f56c089855f35 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"))),
+ 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..49aeb50155b157 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
@@ -76,6 +76,9 @@ struct C {
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: returning a constant reference parameter
};
+const auto Lf1 = [](const T& t) -> const T& { return t; };
+// CHECK-MESSAGES: :[[@LINE-1]]:54: warning: returning a constant reference parameter
+
} // namespace invalid
namespace false_negative_because_dependent_and_not_instantiated {
@@ -151,6 +154,14 @@ void instantiate(const int ¶m, const float ¶mf, 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{};
+}
+
+const auto Lf1 = [](T& t) -> const T& { return t; };
+
} // namespace valid
namespace overload {
More information about the cfe-commits
mailing list