Skip to content

Commit a07e8cd

Browse files
authored
[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.
1 parent fffe8c6 commit a07e8cd

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,20 @@ void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
3131
qualType(lValueReferenceType(pointee(
3232
qualType(isConstQualified()))))
3333
.bind("type"))),
34-
hasDeclContext(functionDecl().bind("owner")),
34+
hasDeclContext(functionDecl(
35+
equalsBoundNode("func"),
36+
hasReturnTypeLoc(loc(qualType(
37+
hasCanonicalType(equalsBoundNode("type"))))))),
3538
unless(hasLifetimeBoundAttr()))
3639
.bind("param")))
3740
.bind("dref"));
38-
const auto Func =
39-
functionDecl(equalsBoundNode("owner"),
40-
hasReturnTypeLoc(loc(
41-
qualType(hasCanonicalType(equalsBoundNode("type"))))))
42-
.bind("func");
4341

4442
Finder->addMatcher(
4543
returnStmt(
44+
hasAncestor(functionDecl().bind("func")),
4645
hasReturnValue(anyOf(
4746
DRef, ignoringParens(conditionalOperator(eachOf(
48-
hasTrueExpression(DRef), hasFalseExpression(DRef)))))),
49-
hasAncestor(Func)),
47+
hasTrueExpression(DRef), hasFalseExpression(DRef))))))),
5048
this);
5149
}
5250

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +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`` and no longer giving
187-
false positives for functions which contain lambda and ignore parameters
186+
by using the conditional operator ``cond ? var1 : var2`` and fixing false
187+
positives for functions which contain lambda and ignore parameters
188188
with ``[[clang::lifetimebound]]`` attribute.
189189

190190
- Improved :doc:`bugprone-sizeof-expression

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,26 @@ namespace use_lifetime_bound_attr {
203203
int const &f(int const &a [[clang::lifetimebound]]) { return a; }
204204
} // namespace use_lifetime_bound_attr
205205
} // namespace gh117696
206+
207+
208+
namespace lambda {
209+
using T = const int &;
210+
using K = const float &;
211+
T inner_valid_lambda(T a) {
212+
[&]() -> T { return a; };
213+
return a;
214+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference parameter
215+
}
216+
T inner_invalid_lambda(T a) {
217+
[&](T a) -> T { return a; };
218+
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: returning a constant reference parameter
219+
return a;
220+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference parameter
221+
}
222+
T inner_invalid_lambda2(T a) {
223+
[&](K a) -> K { return a; };
224+
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: returning a constant reference parameter
225+
return a;
226+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference parameter
227+
}
228+
} // namespace lambda

0 commit comments

Comments
 (0)