7
7
// ===----------------------------------------------------------------------===//
8
8
9
9
#include " ReturnConstRefFromParameterCheck.h"
10
+ #include " clang/AST/Expr.h"
10
11
#include " clang/ASTMatchers/ASTMatchFinder.h"
11
12
#include " clang/ASTMatchers/ASTMatchers.h"
12
13
@@ -15,20 +16,24 @@ using namespace clang::ast_matchers;
15
16
namespace clang ::tidy::bugprone {
16
17
17
18
void 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 );
32
37
}
33
38
34
39
static bool isSameTypeIgnoringConst (QualType A, QualType B) {
@@ -85,8 +90,8 @@ void ReturnConstRefFromParameterCheck::check(
85
90
const MatchFinder::MatchResult &Result) {
86
91
const auto *FD = Result.Nodes .getNodeAs <FunctionDecl>(" func" );
87
92
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 ();
90
95
if (Range.isInvalid ())
91
96
return ;
92
97
0 commit comments