[clang-tools-extra] a07e8cd - [clang-tidy] fix false positive in lambda expr for return-const-ref-from-parameter (#118990)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 6 16:31:37 PST 2024
Author: Congcong Cai
Date: 2024-12-07T08:31:35+08:00
New Revision: a07e8cdae7727583e20c9dec632a376365a6e209
URL: https://github.com/llvm/llvm-project/commit/a07e8cdae7727583e20c9dec632a376365a6e209
DIFF: https://github.com/llvm/llvm-project/commit/a07e8cdae7727583e20c9dec632a376365a6e209.diff
LOG: [clang-tidy] fix false positive in lambda expr for return-const-ref-from-parameter (#118990)
We should bind the node in `hasAncestor` matcher and `equalsBoundNode`
in the other matcher because `hasAncestor` will visit the ancestor until
to find the matched result.
Added:
Modified:
clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
Removed:
################################################################################
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