Skip to content

Commit 7100846

Browse files
committed
[clang-tidy] fix false positive in bugprone-return-const-ref-from-parameter
1 parent f7dc1d0 commit 7100846

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ namespace clang::tidy::bugprone {
1818
void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
1919
const auto DRef = ignoringParens(
2020
declRefExpr(
21-
to(parmVarDecl(hasType(hasCanonicalType(
22-
qualType(lValueReferenceType(pointee(
23-
qualType(isConstQualified()))))
24-
.bind("type"))))
21+
to(parmVarDecl(
22+
hasType(hasCanonicalType(
23+
qualType(lValueReferenceType(
24+
pointee(qualType(isConstQualified()))))
25+
.bind("type"))),
26+
hasDeclContext(functionDecl().bind("owner")))
2527
.bind("param")))
2628
.bind("dref"));
2729
const auto Func =
28-
functionDecl(hasReturnTypeLoc(loc(
30+
functionDecl(equalsBoundNode("owner"),
31+
hasReturnTypeLoc(loc(
2932
qualType(hasCanonicalType(equalsBoundNode("type"))))))
3033
.bind("func");
3134

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ Changes in existing checks
179179
- Improved :doc:`bugprone-return-const-ref-from-parameter
180180
<clang-tidy/checks/bugprone/return-const-ref-from-parameter>` check to
181181
diagnose potential dangling references when returning a ``const &`` parameter
182-
by using the conditional operator ``cond ? var1 : var2``.
182+
by using the conditional operator ``cond ? var1 : var2`` and no longer giving
183+
false positives for lambda.
183184

184185
- Improved :doc:`bugprone-sizeof-expression
185186
<clang-tidy/checks/bugprone/sizeof-expression>` check to find suspicious

clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ struct C {
7676
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: returning a constant reference parameter
7777
};
7878

79+
const auto Lf1 = [](const T& t) -> const T& { return t; };
80+
// CHECK-MESSAGES: :[[@LINE-1]]:54: warning: returning a constant reference parameter
81+
7982
} // namespace invalid
8083

8184
namespace false_negative_because_dependent_and_not_instantiated {
@@ -151,6 +154,14 @@ void instantiate(const int &param, const float &paramf, int &mut_param, float &m
151154
itf6(mut_paramf);
152155
}
153156

157+
template<class T>
158+
void f(const T& t) {
159+
const auto get = [&t] -> const T& { return t; };
160+
return T{};
161+
}
162+
163+
const auto Lf1 = [](T& t) -> const T& { return t; };
164+
154165
} // namespace valid
155166

156167
namespace overload {

0 commit comments

Comments
 (0)