[clang-tools-extra] [clang-tidy] Add check 'bugprone-cast-to-struct' (PR #153428)
Yanzuo Liu via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 21 05:06:39 PST 2026
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/153428 at github.com>
================
@@ -0,0 +1,97 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "CastToStructCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+
+AST_MATCHER(Type, charType) { return Node.isCharType(); }
+AST_MATCHER(Type, unionType) { return Node.isUnionType(); }
+
+} // namespace
+
+CastToStructCheck::CastToStructCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ IgnoredCasts(
+ utils::options::parseStringList(Options.get("IgnoredCasts", ""))) {
+ IgnoredCastsRegex.reserve(IgnoredCasts.size());
+ for (const auto &Str : IgnoredCasts) {
+ std::string WholeWordRegex;
+ WholeWordRegex.reserve(Str.size() + 2);
+ WholeWordRegex.push_back('^');
+ WholeWordRegex.append(Str);
+ WholeWordRegex.push_back('$');
+ IgnoredCastsRegex.emplace_back(WholeWordRegex);
+ }
+}
+
+void CastToStructCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "IgnoredCasts",
+ utils::options::serializeStringList(IgnoredCasts));
+}
+
+void CastToStructCheck::registerMatchers(MatchFinder *Finder) {
+ auto FromPointee =
+ qualType(hasUnqualifiedDesugaredType(type().bind("FromType")),
+ unless(voidType()), unless(charType()), unless(unionType()))
+ .bind("FromPointee");
----------------
zwuis wrote:
Unused binding. Ditto for "ToPointee".
https://github.com/llvm/llvm-project/pull/153428
More information about the cfe-commits
mailing list