Skip to content

Commit

Permalink
Fix false positives when using a typedef in combination with a generic
Browse files Browse the repository at this point in the history
We should not use the underlying CXX record, because that will resolve the typedef

We usually don't do this in our code, but QVector is one example where it has happened (in qt6)
  • Loading branch information
alex1701c committed Mar 11, 2024
1 parent 967fdfe commit ebf99de
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/checks/level0/fully-qualified-moc-types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ static std::string getQualifiedNameOfType(const Type *ptr, const LangOptions &lo
}
if (auto *typedefDecl = ptr->getAs<TypedefType>(); typedefDecl && typedefDecl->getDecl()) {
return typedefDecl->getDecl()->getQualifiedNameAsString();
} else if (auto templateSpec = ptr->getAs<TemplateSpecializationType>()) {
// In case one uses a typedef with generics, like QVector<QString> in Qt6
// The docs indicate getAsTemplateDecl might be null - so be prepared for that
if (auto *decl = templateSpec->getTemplateName().getAsTemplateDecl()) {
return decl->getQualifiedNameAsString();
}
} else if (auto recordDecl = ptr->getAsRecordDecl()) {
return recordDecl->getQualifiedNameAsString();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/fully-qualified-moc-types/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace NS {
}



template<typename T> using DummyListAlias = QList<T>;
namespace { // annonymous
struct AnnonFoo {};
};
Expand Down Expand Up @@ -91,6 +91,7 @@ public Q_SLOTS:
inline void nestedGeneric(QDBusPendingReply<std::shared_ptr<MyObj2>>) {} // OK
inline void nestedNotFullyQualifiedGeneric(QDBusPendingReply<std::shared_ptr<MyList>>) {} // WARN
inline const MyList& notQualWithModifier() {return lst;};
DummyListAlias<int> myList() { return {1,2,3};};
private:
MyList lst;
};
Expand Down
3 changes: 2 additions & 1 deletion tests/fully-qualified-moc-types/main.cpp.fixed.expected
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace NS {
}



template<typename T> using DummyListAlias = QList<T>;
namespace { // annonymous
struct AnnonFoo {};
};
Expand Down Expand Up @@ -91,6 +91,7 @@ public Q_SLOTS:
inline void nestedGeneric(QDBusPendingReply<std::shared_ptr<MyObj2>>) {} // OK
inline void nestedNotFullyQualifiedGeneric(QDBusPendingReply<std::shared_ptr<MyObj2::MyList>>) {} // WARN
inline const MyObj2::MyList& notQualWithModifier() {return lst;};
DummyListAlias<int> myList() { return {1,2,3};};
private:
MyList lst;
};
Expand Down

0 comments on commit ebf99de

Please sign in to comment.