Skip to content

Commit dc8bc27

Browse files
committed
fixed review issues
1 parent 69c37fd commit dc8bc27

File tree

3 files changed

+10
-25
lines changed

3 files changed

+10
-25
lines changed

clang-tools-extra/clang-tidy/bugprone/CastToStructCheck.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ namespace clang::tidy::bugprone {
1717

1818
namespace {
1919

20-
AST_MATCHER(Type, charType) {
21-
return Node.isCharType();
22-
}
23-
AST_MATCHER(Type, unionType) {
24-
return Node.isUnionType();
25-
}
20+
AST_MATCHER(Type, charType) { return Node.isCharType(); }
21+
AST_MATCHER(Type, unionType) { return Node.isUnionType(); }
2622

27-
}
23+
} // namespace
2824

2925
CastToStructCheck::CastToStructCheck(StringRef Name, ClangTidyContext *Context)
3026
: ClangTidyCheck(Name, Context),
@@ -49,9 +45,7 @@ void CastToStructCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
4945
void CastToStructCheck::registerMatchers(MatchFinder *Finder) {
5046
auto FromPointee =
5147
qualType(hasUnqualifiedDesugaredType(type().bind("FromType")),
52-
unless(voidType()),
53-
unless(charType()),
54-
unless(unionType()))
48+
unless(voidType()), unless(charType()), unless(unionType()))
5549
.bind("FromPointee");
5650
auto ToPointee =
5751
qualType(hasUnqualifiedDesugaredType(
@@ -77,7 +71,8 @@ void CastToStructCheck::check(const MatchFinder::MatchResult &Result) {
7771
if (FromType == ToType)
7872
return;
7973

80-
auto CheckNameIgnore = [this](const std::string &FromName, const std::string &ToName) {
74+
auto CheckNameIgnore = [this](const std::string &FromName,
75+
const std::string &ToName) {
8176
bool FromMatch = false;
8277
for (auto [Idx, Regex] : llvm::enumerate(IgnoredCastsRegex)) {
8378
if (Idx % 2 == 0) {
@@ -93,10 +88,9 @@ void CastToStructCheck::check(const MatchFinder::MatchResult &Result) {
9388
if (CheckNameIgnore(FromPtr->getAsString(), ToPtr->getAsString()))
9489
return;
9590

96-
diag(FoundCastExpr->getExprLoc(),
97-
"casting a %0 pointer to a "
98-
"%1 pointer and accessing a field can lead to memory "
99-
"access errors or data corruption")
91+
diag(FoundCastExpr->getExprLoc(), "casting a %0 pointer to a "
92+
"%1 pointer can lead to memory "
93+
"access errors or data corruption")
10094
<< *FromPtr << *ToPtr;
10195
}
10296

clang-tools-extra/clang-tidy/bugprone/CastToStructCheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class CastToStructCheck : public ClangTidyCheck {
2525
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2626
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2727
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
28+
// C++ has different options for casting that make such a check less useful.
2829
return !LangOpts.CPlusPlus;
2930
}
3031

clang-tools-extra/docs/clang-tidy/checks/bugprone/cast-to-struct.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ types.
3434
s = (struct S1 *)calloc(1, sizeof(struct S1)); // no warning
3535
}
3636
37-
Limitations
38-
-----------
39-
40-
The check does not run on `C++` code.
41-
42-
C-style casts are discouraged in C++ and should be converted to more type-safe
43-
casts. The ``reinterpreted_cast`` is used for the most unsafe cases and
44-
indicates by itself a potentially dangerous operation. Additionally, inheritance
45-
and dynamic types would make such a check less useful.
46-
4737
Options
4838
-------
4939

0 commit comments

Comments
 (0)