Skip to content

Commit 04ab599

Browse files
authored
[clang-tidy] fix false positive in bugprone-return-const-ref-from-parameter (#117734)
Fix #115743
1 parent 9df63b2 commit 04ab599

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
2121
to(parmVarDecl(hasType(hasCanonicalType(
2222
qualType(lValueReferenceType(pointee(
2323
qualType(isConstQualified()))))
24-
.bind("type"))))
24+
.bind("type"))),
25+
hasDeclContext(functionDecl().bind("owner")))
2526
.bind("param")))
2627
.bind("dref"));
2728
const auto Func =
28-
functionDecl(hasReturnTypeLoc(loc(
29+
functionDecl(equalsBoundNode("owner"),
30+
hasReturnTypeLoc(loc(
2931
qualType(hasCanonicalType(equalsBoundNode("type"))))))
3032
.bind("func");
3133

clang-tools-extra/docs/ReleaseNotes.rst

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

188189
- Improved :doc:`bugprone-sizeof-expression
189190
<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)