File tree Expand file tree Collapse file tree 3 files changed +31
-10
lines changed Expand file tree Collapse file tree 3 files changed +31
-10
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -203,3 +203,26 @@ namespace use_lifetime_bound_attr {
203203int 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
You can’t perform that action at this time.
0 commit comments