clang-tools 24.0.0git
UnusedLocalNonTrivialVariableCheck.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/Matchers.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/ASTTypeTraits.h"
14#include "clang/AST/Type.h"
15#include "clang/ASTMatchers/ASTMatchFinder.h"
16#include "clang/ASTMatchers/ASTMatchers.h"
17#include "clang/ASTMatchers/ASTMatchersMacros.h"
18#include "clang/Basic/LangOptions.h"
19
20using namespace clang::ast_matchers;
21using namespace clang::tidy::matchers;
22
23namespace clang::tidy::bugprone {
24
25namespace {
26static constexpr StringRef DefaultIncludeTypeRegex =
27 "::std::.*mutex;::std::future;::std::basic_string;::std::basic_regex;"
28 "::std::basic_istringstream;::std::basic_stringstream;::std::bitset;"
29 "::std::filesystem::path";
30
31AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
32AST_MATCHER(VarDecl, explicitMarkUnused) {
33 // Implementations should not emit a warning that a name-independent
34 // declaration is used or unused.
35 const LangOptions &LangOpts = Finder->getASTContext().getLangOpts();
36 return Node.hasAttr<UnusedAttr>() ||
37 (LangOpts.CPlusPlus26 && Node.isPlaceholderVar(LangOpts));
38}
39AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
40AST_MATCHER(QualType, isTrivial) {
41 return Node.isTrivialType(Finder->getASTContext()) ||
42 Node.isTriviallyCopyableType(Finder->getASTContext());
43}
44} // namespace
45
47 StringRef Name, ClangTidyContext *Context)
48 : ClangTidyCheck(Name, Context),
49 IncludeTypes(utils::options::parseStringList(
50 Options.get("IncludeTypes", DefaultIncludeTypeRegex))),
51 ExcludeTypes(
52 utils::options::parseStringList(Options.get("ExcludeTypes", ""))) {}
53
56 Options.store(Opts, "IncludeTypes",
58 Options.store(Opts, "ExcludeTypes",
60}
61
63 if (IncludeTypes.empty())
64 return;
65
66 Finder->addMatcher(
67 varDecl(isLocalVarDecl(), unless(isReferenced()),
68 unless(isExceptionVariable()), hasLocalStorage(), isDefinition(),
69 unless(hasType(isReferenceType())), unless(hasType(isTrivial())),
70 unless(explicitMarkUnused()),
71 hasType(hasUnqualifiedDesugaredType(anyOf(
72 recordType(hasDeclaration(namedDecl(
73 matchesAnyListedRegexName(IncludeTypes),
74 unless(matchesAnyListedRegexName(ExcludeTypes))))),
75 templateSpecializationType(hasDeclaration(namedDecl(
76 matchesAnyListedRegexName(IncludeTypes),
77 unless(matchesAnyListedRegexName(ExcludeTypes)))))))))
78 .bind("var"),
79 this);
80}
81
83 const MatchFinder::MatchResult &Result) {
84 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("var");
85 diag(MatchedDecl->getLocation(), "unused local variable %0 of type %1")
86 << MatchedDecl << MatchedDecl->getType();
87}
88
90 const LangOptions &LangOpts) const {
91 return LangOpts.CPlusPlus;
92}
93
94std::optional<TraversalKind>
96 return TK_IgnoreUnlessSpelledInSource;
97}
98
99} // namespace clang::tidy::bugprone
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
AST_MATCHER(BinaryOperator, isRelationalOperator)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedRegexName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap