Skip to content

Commit 5d35398

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

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-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+
parmVarDecl(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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ void instantiate(const int &param, const float &paramf, int &mut_param, float &m
151151
itf6(mut_paramf);
152152
}
153153

154+
template<class T>
155+
void f(const T& t) {
156+
const auto get = [&t] -> const T& { return t; };
157+
return T{};
158+
}
159+
154160
} // namespace valid
155161

156162
namespace overload {

0 commit comments

Comments
 (0)