77// ===----------------------------------------------------------------------===//
88
99#include " ReturnConstRefFromParameterCheck.h"
10+ #include " clang/AST/Expr.h"
1011#include " clang/ASTMatchers/ASTMatchFinder.h"
1112#include " clang/ASTMatchers/ASTMatchers.h"
1213
@@ -15,20 +16,24 @@ using namespace clang::ast_matchers;
1516namespace clang ::tidy::bugprone {
1617
1718void ReturnConstRefFromParameterCheck::registerMatchers (MatchFinder *Finder) {
18- Finder->addMatcher (
19- returnStmt (
20- hasReturnValue (declRefExpr (
21- to (parmVarDecl (hasType (hasCanonicalType (
22- qualType (lValueReferenceType (pointee (
23- qualType (isConstQualified ()))))
24- .bind (" type" ))))
25- .bind (" param" )))),
26- hasAncestor (
27- functionDecl (hasReturnTypeLoc (loc (qualType (
28- hasCanonicalType (equalsBoundNode (" type" ))))))
29- .bind (" func" )))
30- .bind (" ret" ),
31- this );
19+ const auto DRef =
20+ declRefExpr (
21+ to (parmVarDecl (hasType (hasCanonicalType (
22+ qualType (lValueReferenceType (pointee (
23+ qualType (isConstQualified ()))))
24+ .bind (" type" ))))
25+ .bind (" param" )))
26+ .bind (" dref" );
27+ const auto Func =
28+ functionDecl (hasReturnTypeLoc (loc (
29+ qualType (hasCanonicalType (equalsBoundNode (" type" ))))))
30+ .bind (" func" );
31+
32+ Finder->addMatcher (returnStmt (hasReturnValue (DRef), hasAncestor (Func)), this );
33+ Finder->addMatcher (conditionalOperator (eachOf (hasTrueExpression (DRef),
34+ hasFalseExpression (DRef)),
35+ hasAncestor (Func)),
36+ this );
3237}
3338
3439static bool isSameTypeIgnoringConst (QualType A, QualType B) {
@@ -85,8 +90,8 @@ void ReturnConstRefFromParameterCheck::check(
8590 const MatchFinder::MatchResult &Result) {
8691 const auto *FD = Result.Nodes .getNodeAs <FunctionDecl>(" func" );
8792 const auto *PD = Result.Nodes .getNodeAs <ParmVarDecl>(" param" );
88- const auto *R = Result.Nodes .getNodeAs <ReturnStmt >(" ret " );
89- const SourceRange Range = R-> getRetValue () ->getSourceRange ();
93+ const auto *DRef = Result.Nodes .getNodeAs <DeclRefExpr >(" dref " );
94+ const SourceRange Range = DRef ->getSourceRange ();
9095 if (Range.isInvalid ())
9196 return ;
9297
0 commit comments