misc-unused-parameters: Don't warn on ParmVarDecls in the return type.

As there don't seem to be a good way of formulating a matcher that
finds all pairs of functions and their ParmVarDecls, just match on
functionDecls and iterate over their parameters. This should also be
more efficient as some checks are only performed once per function.

llvm-svn: 243267
This commit is contained in:
Daniel Jasper 2015-07-27 13:46:37 +00:00
parent 5a4892b4fa
commit 9fe55a32b7
3 changed files with 28 additions and 16 deletions

View File

@ -17,9 +17,7 @@ namespace clang {
namespace tidy {
void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
parmVarDecl(hasParent(functionDecl().bind("function"))).bind("x"),
this);
Finder->addMatcher(functionDecl().bind("function"), this);
}
static FixItHint removeParameter(const FunctionDecl *Function, unsigned Index) {
@ -54,15 +52,10 @@ static FixItHint removeArgument(const CallExpr *Call, unsigned Index) {
return FixItHint::CreateRemoval(RemovalRange);
}
void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
if (!Function->doesThisDeclarationHaveABody())
return;
const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("x");
if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
Param->hasAttr<UnusedAttr>())
return;
void UnusedParametersCheck::warnOnUnusedParameter(
const MatchFinder::MatchResult &Result, const FunctionDecl *Function,
unsigned ParamIndex) {
const auto *Param = Function->getParamDecl(ParamIndex);
auto MyDiag = diag(Param->getLocation(), "parameter '%0' is unused")
<< Param->getName();
@ -86,10 +79,6 @@ void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
return;
}
// Handle local functions by deleting the parameters.
unsigned ParamIndex = Param->getFunctionScopeIndex();
assert(ParamIndex < Function->getNumParams());
// Fix all redeclarations.
for (const FunctionDecl *FD : Function->redecls())
MyDiag << removeParameter(FD, ParamIndex);
@ -103,5 +92,18 @@ void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
MyDiag << removeArgument(Match.getNodeAs<CallExpr>("x"), ParamIndex);
}
void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
if (!Function->doesThisDeclarationHaveABody())
return;
for (unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
const auto *Param = Function->getParamDecl(i);
if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
Param->hasAttr<UnusedAttr>())
continue;
warnOnUnusedParameter(Result, Function, i);
}
}
} // namespace tidy
} // namespace clang

View File

@ -23,6 +23,11 @@ public:
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
void
warnOnUnusedParameter(const ast_matchers::MatchFinder::MatchResult &Result,
const FunctionDecl *Function, unsigned ParamIndex);
};
} // namespace tidy

View File

@ -101,3 +101,8 @@ template <typename T> void someFunctionTemplateAllUnusedParams(T b, T e) {}
// CHECK-FIXES: {{^}}template <typename T> void someFunctionTemplateAllUnusedParams(T /*b*/, T /*e*/) {}
static void dontGetConfusedByParametersInFunctionTypes() { void (*F)(int i); }
template <typename T> class Function {};
static Function<void(int, int i)> dontGetConfusedByFunctionReturnTypes() {
return Function<void(int, int)>();
}