I’m trying to implement a clang-tidy check to warn about the use of output parameters.
As registerMatchers method, I have the following:
void AvoidouputparametersCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
functionDecl(isDefinition(), hasBody(stmt()), hasAnyParameter(decl())).bind("function"),
this);
}
As check method, I have the following:
void AvoidouputparametersCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
for (unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
const auto *Param = Function->getParamDecl(i);
if(!Param->getType().getCanonicalType().isConstQualified() && Param->getType().getTypePtr
()->isReferenceType())
auto MyDiag = diag(Param->getLocation(), "avoid output parameters") << Param;
}
If I run clang-tidy I have the following warnings:
void foo(int a &){}
warning: output parameter
void foo(const int &a){}
void foo(int const &a){}
warning: output parameter
When I place const in the right hand side of the type, clang does not detect it as a const qualified parameters.