[clang-tools-extra] [clang-tidy] avoid false positive when overload for bugprone-return-const-ref-from-parameter (PR #95434)
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 14 00:38:42 PDT 2024
================
@@ -18,20 +17,87 @@ namespace clang::tidy::bugprone {
void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
returnStmt(
- hasReturnValue(declRefExpr(to(parmVarDecl(hasType(hasCanonicalType(
- qualType(matchers::isReferenceToConst()).bind("type"))))))),
- hasAncestor(functionDecl(hasReturnTypeLoc(
- loc(qualType(hasCanonicalType(equalsBoundNode("type"))))))))
+ hasReturnValue(declRefExpr(
+ to(parmVarDecl(hasType(hasCanonicalType(
+ qualType(lValueReferenceType(pointee(
+ qualType(isConstQualified()))))
+ .bind("type"))))
+ .bind("param")))),
+ hasAncestor(
+ functionDecl(hasReturnTypeLoc(loc(qualType(
+ hasCanonicalType(equalsBoundNode("type"))))))
+ .bind("func")))
.bind("ret"),
this);
}
+static bool isSameTypeIgnoringConst(QualType A, QualType B) {
+ A = A.getCanonicalType();
+ B = B.getCanonicalType();
+ A.addConst();
+ B.addConst();
+ return A == B;
+}
+
+static bool isSameTypeIgnoringConstRef(QualType A, QualType B) {
+ return isSameTypeIgnoringConst(A.getCanonicalType().getNonReferenceType(),
+ B.getCanonicalType().getNonReferenceType());
+}
+
+static bool hasSameParameterTypes(const FunctionDecl &FD, const FunctionDecl &O,
+ const ParmVarDecl &PD) {
+ if (FD.getNumParams() != O.getNumParams())
+ return false;
+ for (unsigned I = 0, E = FD.getNumParams(); I < E; ++I) {
+ const ParmVarDecl *DPD = FD.getParamDecl(I);
+ const QualType OPT = O.getParamDecl(I)->getType();
+ if (DPD == &PD) {
+ if (!llvm::isa<RValueReferenceType>(OPT) ||
+ !isSameTypeIgnoringConstRef(DPD->getType(), OPT))
+ return false;
+ } else {
+ if (!isSameTypeIgnoringConst(DPD->getType(), OPT))
+ return false;
+ }
+ }
+ return true;
+}
+
+static const Decl *findRVRefOverload(const FunctionDecl &FD,
+ const ParmVarDecl &PD) {
+ // Actually it would be better to do lookup in caller site.
+ // But in most of cases, overloads of LVRef and RVRef will appear together.
+ // FIXME:
+ // 1. overload in anonymous namespace
+ // 2. forward reference
+ DeclContext::lookup_result LookupResult =
+ FD.getParent()->lookup(FD.getNameInfo().getName());
+ if (LookupResult.isSingleResult()) {
+ return nullptr;
+ }
+ for (const Decl *Overload : LookupResult) {
+ if (Overload == &FD)
+ continue;
+ Overload->dumpColor();
----------------
PiotrZSL wrote:
debug ?
https://github.com/llvm/llvm-project/pull/95434
More information about the cfe-commits
mailing list