1 //===--- UnusedLocalNonTrivialVariableCheck.cpp - clang-tidy --------------===//
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
7 //===----------------------------------------------------------------------===//
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"
19 using namespace clang::ast_matchers
;
20 using namespace clang::tidy::matchers
;
22 namespace clang::tidy::bugprone
{
25 static 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";
30 AST_MATCHER(VarDecl
, isLocalVarDecl
) { return Node
.isLocalVarDecl(); }
31 AST_MATCHER(VarDecl
, isReferenced
) { return Node
.isReferenced(); }
32 AST_MATCHER(Type
, isReferenceType
) { return Node
.isReferenceType(); }
33 AST_MATCHER(QualType
, isTrivial
) {
34 return Node
.isTrivialType(Finder
->getASTContext()) ||
35 Node
.isTriviallyCopyableType(Finder
->getASTContext());
39 UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck(
40 StringRef Name
, ClangTidyContext
*Context
)
41 : ClangTidyCheck(Name
, Context
),
42 IncludeTypes(utils::options::parseStringList(
43 Options
.get("IncludeTypes", DefaultIncludeTypeRegex
))),
45 utils::options::parseStringList(Options
.get("ExcludeTypes", ""))) {}
47 void UnusedLocalNonTrivialVariableCheck::storeOptions(
48 ClangTidyOptions::OptionMap
&Opts
) {
49 Options
.store(Opts
, "IncludeTypes",
50 utils::options::serializeStringList(IncludeTypes
));
51 Options
.store(Opts
, "ExcludeTypes",
52 utils::options::serializeStringList(ExcludeTypes
));
55 void UnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder
*Finder
) {
56 if (IncludeTypes
.empty())
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
)))))))))
75 void UnusedLocalNonTrivialVariableCheck::check(
76 const MatchFinder::MatchResult
&Result
) {
77 const auto *MatchedDecl
= Result
.Nodes
.getNodeAs
<VarDecl
>("var");
78 diag(MatchedDecl
->getLocation(), "unused local variable %0 of type %1")
79 << MatchedDecl
<< MatchedDecl
->getType();
82 bool UnusedLocalNonTrivialVariableCheck::isLanguageVersionSupported(
83 const LangOptions
&LangOpts
) const {
84 return LangOpts
.CPlusPlus
;
87 std::optional
<TraversalKind
>
88 UnusedLocalNonTrivialVariableCheck::getCheckTraversalKind() const {
89 return TK_IgnoreUnlessSpelledInSource
;
92 } // namespace clang::tidy::bugprone