Skip to content

Commit ff59ecd

Browse files
authored
[clang-tidy] Rename google-readability-casting to modernize-avoid-c-style-cast (#171058)
Rename `google-readability-casting` to `modernize-avoid-c-style-cast` The old name is kept as an alias for backward compatibility. Fixes #170978
1 parent ca12d1d commit ff59ecd

17 files changed

+104
-63
lines changed

clang-tools-extra/clang-tidy/google/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ set(LLVM_LINK_COMPONENTS
44
)
55

66
add_clang_library(clangTidyGoogleModule STATIC
7-
AvoidCStyleCastsCheck.cpp
87
AvoidNSObjectNewCheck.cpp
98
AvoidThrowingObjCExceptionCheck.cpp
109
AvoidUnderscoreInGoogletestNameCheck.cpp

clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#include "../ClangTidy.h"
1010
#include "../ClangTidyModule.h"
1111
#include "../ClangTidyModuleRegistry.h"
12+
#include "../modernize/AvoidCStyleCastCheck.h"
1213
#include "../readability/BracesAroundStatementsCheck.h"
1314
#include "../readability/FunctionSizeCheck.h"
1415
#include "../readability/NamespaceCommentCheck.h"
15-
#include "AvoidCStyleCastsCheck.h"
1616
#include "AvoidNSObjectNewCheck.h"
1717
#include "AvoidThrowingObjCExceptionCheck.h"
1818
#include "AvoidUnderscoreInGoogletestNameCheck.h"
@@ -67,7 +67,7 @@ class GoogleModule : public ClangTidyModule {
6767
CheckFactories
6868
.registerCheck<readability::AvoidUnderscoreInGoogletestNameCheck>(
6969
"google-readability-avoid-underscore-in-googletest-name");
70-
CheckFactories.registerCheck<readability::AvoidCStyleCastsCheck>(
70+
CheckFactories.registerCheck<modernize::AvoidCStyleCastCheck>(
7171
"google-readability-casting");
7272
CheckFactories.registerCheck<readability::TodoCommentCheck>(
7373
"google-readability-todo");

clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp renamed to clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "AvoidCStyleCastsCheck.h"
9+
#include "AvoidCStyleCastCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
1212
#include "clang/ASTMatchers/ASTMatchers.h"
1313
#include "clang/Lex/Lexer.h"
1414

1515
using namespace clang::ast_matchers;
1616

17-
namespace clang::tidy::google::readability {
17+
namespace clang::tidy::modernize {
1818

19-
void AvoidCStyleCastsCheck::registerMatchers(
20-
ast_matchers::MatchFinder *Finder) {
19+
void AvoidCStyleCastCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
2120
Finder->addMatcher(
2221
cStyleCastExpr(
2322
// Filter out (EnumType)IntegerLiteral construct, which is generated
@@ -113,7 +112,7 @@ static bool sameTypeAsWritten(QualType X, QualType Y) {
113112
}
114113
}
115114

116-
void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
115+
void AvoidCStyleCastCheck::check(const MatchFinder::MatchResult &Result) {
117116
const auto *CastExpr = Result.Nodes.getNodeAs<ExplicitCastExpr>("cast");
118117

119118
// Ignore casts in macros.
@@ -288,4 +287,4 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
288287
Diag << "static_cast/const_cast/reinterpret_cast";
289288
}
290289

291-
} // namespace clang::tidy::google::readability
290+
} // namespace clang::tidy::modernize

clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.h renamed to clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDCSTYLECASTSCHECK_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDCSTYLECASTSCHECK_H
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDCSTYLECASTCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDCSTYLECASTCHECK_H
1111

1212
#include "../ClangTidyCheck.h"
1313

14-
namespace clang::tidy::google::readability {
14+
namespace clang::tidy::modernize {
1515

1616
/// Finds usages of C-style casts.
1717
///
@@ -24,10 +24,10 @@ namespace clang::tidy::google::readability {
2424
/// ones generated by `-Wold-style-cast`.
2525
///
2626
/// For the user-facing documentation see:
27-
/// https://clang.llvm.org/extra/clang-tidy/checks/google/readability-casting.html
28-
class AvoidCStyleCastsCheck : public ClangTidyCheck {
27+
/// https://clang.llvm.org/extra/clang-tidy/checks/modernize/avoid-c-style-cast.html
28+
class AvoidCStyleCastCheck : public ClangTidyCheck {
2929
public:
30-
AvoidCStyleCastsCheck(StringRef Name, ClangTidyContext *Context)
30+
AvoidCStyleCastCheck(StringRef Name, ClangTidyContext *Context)
3131
: ClangTidyCheck(Name, Context) {}
3232
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
3333
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
@@ -36,6 +36,6 @@ class AvoidCStyleCastsCheck : public ClangTidyCheck {
3636
}
3737
};
3838

39-
} // namespace clang::tidy::google::readability
39+
} // namespace clang::tidy::modernize
4040

41-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDCSTYLECASTSCHECK_H
41+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDCSTYLECASTCHECK_H

clang-tools-extra/clang-tidy/modernize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
66
add_clang_library(clangTidyModernizeModule STATIC
77
AvoidBindCheck.cpp
88
AvoidCArraysCheck.cpp
9+
AvoidCStyleCastCheck.cpp
910
AvoidSetjmpLongjmpCheck.cpp
1011
AvoidVariadicFunctionsCheck.cpp
1112
ConcatNestedNamespacesCheck.cpp

clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../ClangTidyModuleRegistry.h"
1212
#include "AvoidBindCheck.h"
1313
#include "AvoidCArraysCheck.h"
14+
#include "AvoidCStyleCastCheck.h"
1415
#include "AvoidSetjmpLongjmpCheck.h"
1516
#include "AvoidVariadicFunctionsCheck.h"
1617
#include "ConcatNestedNamespacesCheck.h"
@@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule {
6566
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
6667
CheckFactories.registerCheck<AvoidBindCheck>("modernize-avoid-bind");
6768
CheckFactories.registerCheck<AvoidCArraysCheck>("modernize-avoid-c-arrays");
69+
CheckFactories.registerCheck<AvoidCStyleCastCheck>(
70+
"modernize-avoid-c-style-cast");
6871
CheckFactories.registerCheck<AvoidSetjmpLongjmpCheck>(
6972
"modernize-avoid-setjmp-longjmp");
7073
CheckFactories.registerCheck<AvoidVariadicFunctionsCheck>(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ New check aliases
328328
<clang-tidy/checks/bugprone/copy-constructor-mutates-argument>`
329329
keeping initial check as an alias to the new one.
330330

331+
- Renamed :doc:`google-readability-casting <clang-tidy/checks/google/readability-casting>` to
332+
:doc:`modernize-avoid-c-style-cast
333+
<clang-tidy/checks/modernize/avoid-c-style-cast>`
334+
keeping initial check as an alias to the new one.
335+
331336
Changes in existing checks
332337
^^^^^^^^^^^^^^^^^^^^^^^^^^
333338

clang-tools-extra/docs/clang-tidy/checks/google/readability-casting.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
google-readability-casting
44
==========================
55

6+
The `google-readability-casting` check is an alias, please see
7+
:doc:`modernize-avoid-c-style-cast <../modernize/avoid-c-style-cast>`
8+
for more information.
9+
610
Finds usages of C-style casts.
711

812
https://google.github.io/styleguide/cppguide.html#Casting
9-
10-
Corresponding cpplint.py check name: `readability/casting`.
11-
12-
This check is similar to ``-Wold-style-cast``, but it suggests automated fixes
13-
in some cases. The reported locations should not be different from the
14-
ones generated by ``-Wold-style-cast``.

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ Clang-Tidy Checks
239239
:doc:`google-objc-function-naming <google/objc-function-naming>`,
240240
:doc:`google-objc-global-variable-declaration <google/objc-global-variable-declaration>`,
241241
:doc:`google-readability-avoid-underscore-in-googletest-name <google/readability-avoid-underscore-in-googletest-name>`,
242-
:doc:`google-readability-casting <google/readability-casting>`,
243242
:doc:`google-readability-todo <google/readability-todo>`,
244243
:doc:`google-runtime-float <google/runtime-float>`,
245244
:doc:`google-runtime-int <google/runtime-int>`,
@@ -291,6 +290,7 @@ Clang-Tidy Checks
291290
:doc:`misc-use-internal-linkage <misc/use-internal-linkage>`, "Yes"
292291
:doc:`modernize-avoid-bind <modernize/avoid-bind>`, "Yes"
293292
:doc:`modernize-avoid-c-arrays <modernize/avoid-c-arrays>`,
293+
:doc:`modernize-avoid-c-style-cast <modernize/avoid-c-style-cast>`,
294294
:doc:`modernize-avoid-setjmp-longjmp <modernize/avoid-setjmp-longjmp>`,
295295
:doc:`modernize-avoid-variadic-functions <modernize/avoid-variadic-functions>`,
296296
:doc:`modernize-concat-nested-namespaces <modernize/concat-nested-namespaces>`, "Yes"
@@ -585,6 +585,7 @@ Check aliases
585585
:doc:`cppcoreguidelines-use-default-member-init <cppcoreguidelines/use-default-member-init>`, :doc:`modernize-use-default-member-init <modernize/use-default-member-init>`, "Yes"
586586
:doc:`fuchsia-header-anon-namespaces <fuchsia/header-anon-namespaces>`, :doc:`google-build-namespaces <google/build-namespaces>`,
587587
:doc:`google-readability-braces-around-statements <google/readability-braces-around-statements>`, :doc:`readability-braces-around-statements <readability/braces-around-statements>`, "Yes"
588+
:doc:`google-readability-casting <google/readability-casting>`, :doc:`modernize-avoid-c-style-cast <modernize/avoid-c-style-cast>`,
588589
:doc:`google-readability-function-size <google/readability-function-size>`, :doc:`readability-function-size <readability/function-size>`,
589590
:doc:`google-readability-namespace-comments <google/readability-namespace-comments>`, :doc:`llvm-namespace-comment <llvm/namespace-comment>`,
590591
:doc:`hicpp-avoid-c-arrays <hicpp/avoid-c-arrays>`, :doc:`modernize-avoid-c-arrays <modernize/avoid-c-arrays>`,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.. title:: clang-tidy - modernize-avoid-c-style-cast
2+
3+
modernize-avoid-c-style-cast
4+
============================
5+
6+
7+
C-style casts can perform a variety of different conversions (``const_cast``,
8+
``static_cast``, ``reinterpret_cast``, or a combination). This makes them
9+
dangerous as the intent is not clear, and they can silently perform unsafe
10+
conversions between incompatible types.
11+
12+
This check is similar to `-Wold-style-cast`, but it suggests automated fixes
13+
in some cases. The reported locations should not be different from the ones
14+
generated by `-Wold-style-cast`.
15+
16+
Examples
17+
--------
18+
19+
.. code-block:: c++
20+
21+
class A {
22+
public:
23+
std::string v;
24+
};
25+
26+
A a;
27+
double *num = (double*)(&a); // Compiles! Hides danger
28+
// num = static_cast<double*>(&a); // Won't compile (good!)
29+
num = reinterpret_cast<double*>(&a); // Compiles, danger is explicit
30+
31+
32+
References
33+
----------
34+
35+
Corresponding cpplint.py check name: `readability/casting`.

0 commit comments

Comments
 (0)