clang-tools 23.0.0git
RedundantVoidArgCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/LexerUtils.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::modernize {
16
17void RedundantVoidArgCheck::registerMatchers(MatchFinder *Finder) {
18 if (getLangOpts().CPlusPlus) {
19 Finder->addMatcher(
20 functionTypeLoc(unless(hasParent(functionDecl(isExternC()))))
21 .bind("fn"),
22 this);
23 Finder->addMatcher(lambdaExpr().bind("fn"), this);
24 } else {
25 Finder->addMatcher(functionTypeLoc().bind("fn"), this);
26 }
27}
28
29void RedundantVoidArgCheck::check(const MatchFinder::MatchResult &Result) {
30 const FunctionTypeLoc TL = [&] {
31 if (const auto *TL = Result.Nodes.getNodeAs<FunctionTypeLoc>("fn"))
32 return *TL;
33 return Result.Nodes.getNodeAs<LambdaExpr>("fn")
35 ->getFunctionTypeLoc();
36 }();
37
38 if (TL.getNumParams() != 0)
39 return;
40
41 const std::optional<Token> Tok = utils::lexer::findNextTokenSkippingComments(
42 Result.SourceManager->getSpellingLoc(TL.getLParenLoc()),
43 *Result.SourceManager, getLangOpts());
44
45 if (!Tok || Tok->isNot(tok::raw_identifier) ||
46 Tok->getRawIdentifier() != "void")
47 return;
48
49 diag(Tok->getLocation(), "redundant void argument list")
50 << FixItHint::CreateRemoval(Tok->getLocation());
51}
52
53} // namespace clang::tidy::modernize
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static const FunctionDecl * getCallOperator(const CXXRecordDecl *Callable, size_t NumArgs)
std::optional< Token > findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition LexerUtils.h:106