[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 21 09:39:44 PST 2023
================
@@ -0,0 +1,49 @@
+//===--- MustUseCheck.cpp - clang-tidy ------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "MustUseCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::misc {
+
+namespace {
+AST_MATCHER(VarDecl, isLocalVar) { return Node.isLocalVarDecl(); }
+AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
+} // namespace
+
+MustUseCheck::MustUseCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ Types(utils::options::parseStringList(Options.get("Types", ""))) {}
+
+void MustUseCheck::registerMatchers(MatchFinder *Finder) {
+ if (Types.empty()) {
+ return;
+ }
+ Finder->addMatcher(
+ varDecl(isLocalVar(), unless(isReferenced()),
+ hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
+ recordDecl(matchers::matchesAnyListedName(Types)))))))
+ .bind("var"),
+ this);
+}
+
+void MustUseCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("var");
+ diag(MatchedDecl->getLocation(), "variable %0 must be used") << MatchedDecl;
----------------
PiotrZSL wrote:
print type in this message
https://github.com/llvm/llvm-project/pull/76101
More information about the cfe-commits
mailing list