https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/117734
>From 528bc58e7abf3eed25ca4921d968e61b52571596 Mon Sep 17 00:00:00 2001 From: Qizhi Hu <836744...@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 | 6 ++++-- clang-tools-extra/docs/ReleaseNotes.rst | 3 ++- .../bugprone/return-const-ref-from-parameter.cpp | 11 +++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp index 7cc4fe519d3a64..7da27c0474d519 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp @@ -21,11 +21,13 @@ void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) { to(parmVarDecl(hasType(hasCanonicalType( qualType(lValueReferenceType(pointee( qualType(isConstQualified())))) - .bind("type")))) + .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..2f2189d6850e2b 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 functions which contain 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 { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits