1//===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy --------------===//
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
9#include "UnusedLocalNonTrivialVariableCheck.h"
10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.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
19using namespace clang::ast_matchers;
20using namespace clang::tidy::matchers;
21
22namespace clang::tidy::bugprone {
23
24namespace {
25static constexpr StringRef DefaultIncludeTypeRegex =
26 "::std::.*mutex;::std::future;::std::basic_string;::std::basic_regex;"
27 "::std::basic_istringstream;::std::basic_stringstream;::std::bitset;"
28 "::std::filesystem::path";
29
30AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
31AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
32AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
33AST_MATCHER(QualType, isTrivial) {
34 return Node.isTrivialType(Context: Finder->getASTContext()) ||
35 Node.isTriviallyCopyableType(Context: Finder->getASTContext());
36}
37} // namespace
38
39UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
40 StringRef Name, ClangTidyContext *Context)
41 : ClangTidyCheck(Name, Context),
42 IncludeTypes(utils::options::parseStringList(
43 Option: Options.get(LocalName: "IncludeTypes", Default: DefaultIncludeTypeRegex))),
44 ExcludeTypes(
45 utils::options::parseStringList(Option: Options.get(LocalName: "ExcludeTypes", Default: ""))) {}
46
47void UnusedLocalNonTrivialVariableCheck::storeOptions(
48 ClangTidyOptions::OptionMap &Opts) {
49 Options.store(Options&: Opts, LocalName: "IncludeTypes",
50 Value: utils::options::serializeStringList(Strings: IncludeTypes));
51 Options.store(Options&: Opts, LocalName: "ExcludeTypes",
52 Value: utils::options::serializeStringList(Strings: ExcludeTypes));
53}
54
55void UnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder *Finder) {
56 if (IncludeTypes.empty())
57 return;
58
59 Finder->addMatcher(
60 varDecl(isLocalVarDecl(), unless(isReferenced()),
61 unless(isExceptionVariable()), hasLocalStorage(), isDefinition(),
62 unless(hasType(isReferenceType())), unless(hasType(isTrivial())),
63 unless(hasAttr(attr::Kind::Unused)),
64 hasType(hasUnqualifiedDesugaredType(
65 anyOf(recordType(hasDeclaration(namedDecl(
66 matchesAnyListedName(IncludeTypes),
67 unless(matchesAnyListedName(ExcludeTypes))))),
68 templateSpecializationType(hasDeclaration(namedDecl(
69 matchesAnyListedName(IncludeTypes),
70 unless(matchesAnyListedName(ExcludeTypes)))))))))
71 .bind("var"),
72 this);
73}
74
75void UnusedLocalNonTrivialVariableCheck::check(
76 const MatchFinder::MatchResult &Result) {
77 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>(ID: "var");
78 diag(MatchedDecl->getLocation(), "unused local variable %0 of type %1")
79 << MatchedDecl << MatchedDecl->getType();
80}
81
82bool UnusedLocalNonTrivialVariableCheck::isLanguageVersionSupported(
83 const LangOptions &LangOpts) const {
84 return LangOpts.CPlusPlus;
85}
86
87std::optional<TraversalKind>
88UnusedLocalNonTrivialVariableCheck::getCheckTraversalKind() const {
89 return TK_IgnoreUnlessSpelledInSource;
90}
91
92} // namespace clang::tidy::bugprone
93

source code of clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp