Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
to(parmVarDecl(hasType(hasCanonicalType(
qualType(lValueReferenceType(pointee(
qualType(isConstQualified()))))
.bind("type"))))
.bind("type"))),
hasDeclContext(functionDecl().bind("owner")))
.bind("param")))
.bind("dref"));
const auto Func =
functionDecl(hasReturnTypeLoc(loc(
functionDecl(equalsBoundNode("owner"),
hasReturnTypeLoc(loc(
qualType(hasCanonicalType(equalsBoundNode("type"))))))
.bind("func");

Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ Changes in existing checks
- Improved :doc:`bugprone-return-const-ref-from-parameter
<clang-tidy/checks/bugprone/return-const-ref-from-parameter>` check to
diagnose potential dangling references when returning a ``const &`` parameter
by using the conditional operator ``cond ? var1 : var2``.
by using the conditional operator ``cond ? var1 : var2`` and no longer giving
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure it is a fix of false positives. Maybe in generic, it more looks like add support of lambda function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function in this issue contains lambda and thus gives a false positive(because it should not report diagnose since the return statement is not belongs to current function). I have modified the release note and make it easier to be understood.

false positives for functions which contain lambda.

- Improved :doc:`bugprone-sizeof-expression
<clang-tidy/checks/bugprone/sizeof-expression>` check to find suspicious
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ struct C {
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: returning a constant reference parameter
};

const auto Lf1 = [](const T& t) -> const T& { return t; };
// CHECK-MESSAGES: :[[@LINE-1]]:54: warning: returning a constant reference parameter

} // namespace invalid

namespace false_negative_because_dependent_and_not_instantiated {
Expand Down Expand Up @@ -151,6 +154,14 @@ void instantiate(const int &param, const float &paramf, int &mut_param, float &m
itf6(mut_paramf);
}

template<class T>
void f(const T& t) {
const auto get = [&t] -> const T& { return t; };
return T{};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test:

const auto get = [](const T& t2) -> const T& { return t2; };

and verify that it's being still detected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


const auto Lf1 = [](T& t) -> const T& { return t; };

} // namespace valid

namespace overload {
Expand Down