[clang-tools-extra] [clang-tidy] fix false positive in bugprone-return-const-ref-from-parameter (PR #117734)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 26 08:27:03 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Qizhi Hu (jcsxky)
<details>
<summary>Changes</summary>
Fix https://github.com/llvm/llvm-project/issues/115743
---
Full diff: https://github.com/llvm/llvm-project/pull/117734.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp (+2-2)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp (+6)
``````````diff
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 ¶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{};
+}
+
} // namespace valid
namespace overload {
``````````
</details>
https://github.com/llvm/llvm-project/pull/117734
More information about the cfe-commits
mailing list