Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clang-tools-extra/clang-tidy/google/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ set(LLVM_LINK_COMPONENTS
)

add_clang_library(clangTidyGoogleModule STATIC
AvoidCStyleCastsCheck.cpp
AvoidNSObjectNewCheck.cpp
AvoidThrowingObjCExceptionCheck.cpp
AvoidUnderscoreInGoogletestNameCheck.cpp
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "../modernize/AvoidCStyleCastCheck.h"
#include "../readability/BracesAroundStatementsCheck.h"
#include "../readability/FunctionSizeCheck.h"
#include "../readability/NamespaceCommentCheck.h"
#include "AvoidCStyleCastsCheck.h"
#include "AvoidNSObjectNewCheck.h"
#include "AvoidThrowingObjCExceptionCheck.h"
#include "AvoidUnderscoreInGoogletestNameCheck.h"
Expand Down Expand Up @@ -67,7 +67,7 @@ class GoogleModule : public ClangTidyModule {
CheckFactories
.registerCheck<readability::AvoidUnderscoreInGoogletestNameCheck>(
"google-readability-avoid-underscore-in-googletest-name");
CheckFactories.registerCheck<readability::AvoidCStyleCastsCheck>(
CheckFactories.registerCheck<modernize::AvoidCStyleCastCheck>(
"google-readability-casting");
CheckFactories.registerCheck<readability::TodoCommentCheck>(
"google-readability-todo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
//
//===----------------------------------------------------------------------===//

#include "AvoidCStyleCastsCheck.h"
#include "AvoidCStyleCastCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"

using namespace clang::ast_matchers;

namespace clang::tidy::google::readability {
namespace clang::tidy::modernize {

void AvoidCStyleCastsCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
void AvoidCStyleCastCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
Finder->addMatcher(
cStyleCastExpr(
// Filter out (EnumType)IntegerLiteral construct, which is generated
Expand Down Expand Up @@ -113,7 +112,7 @@ static bool sameTypeAsWritten(QualType X, QualType Y) {
}
}

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

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

} // namespace clang::tidy::google::readability
} // namespace clang::tidy::modernize
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDCSTYLECASTSCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDCSTYLECASTSCHECK_H
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDCSTYLECASTCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDCSTYLECASTCHECK_H

#include "../ClangTidyCheck.h"

namespace clang::tidy::google::readability {
namespace clang::tidy::modernize {

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

} // namespace clang::tidy::google::readability
} // namespace clang::tidy::modernize

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOIDCSTYLECASTSCHECK_H
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDCSTYLECASTCHECK_H
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
add_clang_library(clangTidyModernizeModule STATIC
AvoidBindCheck.cpp
AvoidCArraysCheck.cpp
AvoidCStyleCastCheck.cpp
AvoidSetjmpLongjmpCheck.cpp
AvoidVariadicFunctionsCheck.cpp
ConcatNestedNamespacesCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "../ClangTidyModuleRegistry.h"
#include "AvoidBindCheck.h"
#include "AvoidCArraysCheck.h"
#include "AvoidCStyleCastCheck.h"
#include "AvoidSetjmpLongjmpCheck.h"
#include "AvoidVariadicFunctionsCheck.h"
#include "ConcatNestedNamespacesCheck.h"
Expand Down Expand Up @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule {
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<AvoidBindCheck>("modernize-avoid-bind");
CheckFactories.registerCheck<AvoidCArraysCheck>("modernize-avoid-c-arrays");
CheckFactories.registerCheck<AvoidCStyleCastCheck>(
"modernize-avoid-c-style-cast");
CheckFactories.registerCheck<AvoidSetjmpLongjmpCheck>(
"modernize-avoid-setjmp-longjmp");
CheckFactories.registerCheck<AvoidVariadicFunctionsCheck>(
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ New check aliases
<clang-tidy/checks/bugprone/copy-constructor-mutates-argument>`
keeping initial check as an alias to the new one.

- Renamed :doc:`google-readability-casting <clang-tidy/checks/google/readability-casting>` to
:doc:`modernize-avoid-c-style-cast
<clang-tidy/checks/modernize/avoid-c-style-cast>`
keeping initial check as an alias to the new one.

Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
google-readability-casting
==========================

The `google-readability-casting` check is an alias, please see
:doc:`modernize-avoid-c-style-cast <../modernize/avoid-c-style-cast>`
for more information.

Finds usages of C-style casts.

https://google.github.io/styleguide/cppguide.html#Casting

Corresponding cpplint.py check name: `readability/casting`.
Copy link
Contributor

@vbvictor vbvictor Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cpplint.py should be left in readability-casting.rst because it is google's tool and I see no reason to remove it from here.


This check is similar to ``-Wold-style-cast``, but it suggests automated fixes
in some cases. The reported locations should not be different from the
ones generated by ``-Wold-style-cast``.
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/clang-tidy/checks/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ Clang-Tidy Checks
:doc:`google-objc-function-naming <google/objc-function-naming>`,
:doc:`google-objc-global-variable-declaration <google/objc-global-variable-declaration>`,
:doc:`google-readability-avoid-underscore-in-googletest-name <google/readability-avoid-underscore-in-googletest-name>`,
:doc:`google-readability-casting <google/readability-casting>`,
:doc:`google-readability-todo <google/readability-todo>`,
:doc:`google-runtime-float <google/runtime-float>`,
:doc:`google-runtime-int <google/runtime-int>`,
Expand Down Expand Up @@ -291,6 +290,7 @@ Clang-Tidy Checks
:doc:`misc-use-internal-linkage <misc/use-internal-linkage>`, "Yes"
:doc:`modernize-avoid-bind <modernize/avoid-bind>`, "Yes"
:doc:`modernize-avoid-c-arrays <modernize/avoid-c-arrays>`,
:doc:`modernize-avoid-c-style-cast <modernize/avoid-c-style-cast>`,
:doc:`modernize-avoid-setjmp-longjmp <modernize/avoid-setjmp-longjmp>`,
:doc:`modernize-avoid-variadic-functions <modernize/avoid-variadic-functions>`,
:doc:`modernize-concat-nested-namespaces <modernize/concat-nested-namespaces>`, "Yes"
Expand Down Expand Up @@ -585,6 +585,7 @@ Check aliases
:doc:`cppcoreguidelines-use-default-member-init <cppcoreguidelines/use-default-member-init>`, :doc:`modernize-use-default-member-init <modernize/use-default-member-init>`, "Yes"
:doc:`fuchsia-header-anon-namespaces <fuchsia/header-anon-namespaces>`, :doc:`google-build-namespaces <google/build-namespaces>`,
:doc:`google-readability-braces-around-statements <google/readability-braces-around-statements>`, :doc:`readability-braces-around-statements <readability/braces-around-statements>`, "Yes"
:doc:`google-readability-casting <google/readability-casting>`, :doc:`modernize-avoid-c-style-cast <modernize/avoid-c-style-cast>`,
:doc:`google-readability-function-size <google/readability-function-size>`, :doc:`readability-function-size <readability/function-size>`,
:doc:`google-readability-namespace-comments <google/readability-namespace-comments>`, :doc:`llvm-namespace-comment <llvm/namespace-comment>`,
:doc:`hicpp-avoid-c-arrays <hicpp/avoid-c-arrays>`, :doc:`modernize-avoid-c-arrays <modernize/avoid-c-arrays>`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.. title:: clang-tidy - modernize-avoid-c-style-cast

modernize-avoid-c-style-cast
============================

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Finds usages of C-style casts.

Forgot the initial line


C-style casts can perform a variety of different conversions (``const_cast``,
``static_cast``, ``reinterpret_cast``, or a combination). This makes them
dangerous as the intent is not clear, and they can silently perform unsafe
conversions between incompatible types.

This check is similar to `-Wold-style-cast`, but it suggests automated fixes
in some cases. The reported locations should not be different from the ones
generated by `-Wold-style-cast`.

Examples
--------

.. code-block:: c++

class A {
public:
std::string v;
};

A a;
double *num = (double*)(&a); // Compiles! Hides danger
// num = static_cast<double*>(&a); // Won't compile (good!)
num = reinterpret_cast<double*>(&a); // Compiles, danger is explicit


References
----------

Corresponding cpplint.py check name: `readability/casting`.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %check_clang_tidy %s modernize-avoid-c-style-cast %t -- -- -x c
// The testing script always adds .cpp extension to the input file name, so we
// need to run clang-tidy directly in order to verify handling of .c files:
// RUN: clang-tidy --checks=-*,modernize-avoid-c-style-cast %s -- -x c++ | FileCheck %s -check-prefix=CHECK-MESSAGES -implicit-check-not='{{warning|error}}:'
// RUN: cp %s %t.main_file.cpp
// RUN: clang-tidy --checks=-*,modernize-avoid-c-style-cast -header-filter='.*' %t.main_file.cpp -- -I%S -DTEST_INCLUDE -x c++ | FileCheck %s -check-prefix=CHECK-MESSAGES -implicit-check-not='{{warning|error}}:'

#ifdef TEST_INCLUDE

#undef TEST_INCLUDE
#include "avoid-c-style-cast.c"

#else

void f(const char *cpc) {
const char *cpc2 = (const char*)cpc;
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type [modernize-avoid-c-style-cast]
// CHECK-FIXES: const char *cpc2 = cpc;
char *pc = (char*)cpc;
typedef const char *Typedef1;
(Typedef1)cpc;
}

#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy -std=c++11-or-later %s google-readability-casting %t -- -- -fexceptions
// RUN: %check_clang_tidy -std=c++11-or-later %s modernize-avoid-c-style-cast %t -- -- -fexceptions

bool g() { return false; }

Expand All @@ -8,14 +8,14 @@ struct Y : public X {};

void f(int a, double b, const char *cpc, const void *cpv, X *pX) {
const char *cpc2 = (const char*)cpc;
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type [google-readability-casting]
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type [modernize-avoid-c-style-cast]
// CHECK-FIXES: const char *cpc2 = cpc;

typedef const char *Typedef1;
typedef const char *Typedef2;
Typedef1 t1;
(Typedef2)t1;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C-style casts are discouraged; use static_cast (if needed, the cast may be redundant) [google-readability-casting]
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C-style casts are discouraged; use static_cast (if needed, the cast may be redundant) [modernize-avoid-c-style-cast]
// CHECK-FIXES: static_cast<Typedef2>(t1);
(const char*)t1;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast (if needed
Expand All @@ -28,7 +28,7 @@ void f(int a, double b, const char *cpc, const void *cpv, X *pX) {
// CHECK-FIXES: t1;

char *pc = (char*)cpc;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use const_cast [google-readability-casting]
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use const_cast [modernize-avoid-c-style-cast]
// CHECK-FIXES: char *pc = const_cast<char*>(cpc);
typedef char Char;
Char *pChar = (Char*)pc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// RUN: not clang-tidy %s --checks='-*,google-explicit-constructor,google-readability-casting' 2>&1 | FileCheck %s
// RUN: not clang-tidy %s --checks='-*,google-explicit-constructor,modernize-avoid-c-style-cast' 2>&1 | FileCheck %s

// NOLINTBEGIN(google-explicit-constructor)
// NOLINTBEGIN(google-readability-casting)
// NOLINTBEGIN(modernize-avoid-c-style-cast)
class A { A(int i); };
auto Num = (unsigned int)(-1);
// NOLINTEND(google-explicit-constructor)
// NOLINTEND(google-readability-casting)
// NOLINTEND(modernize-avoid-c-style-cast)

// Note: the expected output has been split over several lines so that clang-tidy
// does not see the "no lint" suppression comment and mistakenly assume it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// RUN: not clang-tidy %s --checks='-*,google-explicit-constructor,google-readability-casting' 2>&1 | FileCheck %s
// RUN: not clang-tidy %s --checks='-*,google-explicit-constructor,modernize-avoid-c-style-cast' 2>&1 | FileCheck %s

// NOLINTBEGIN(google-explicit-constructor,google-readability-casting)
// NOLINTBEGIN(google-explicit-constructor,modernize-avoid-c-style-cast)
class B { B(int i); };
// NOLINTEND(google-explicit-constructor)
auto Num2 = (unsigned int)(-1);
// NOLINTEND(google-readability-casting)
// NOLINTEND(modernize-avoid-c-style-cast)

// Note: the expected output has been split over several lines so that clang-tidy
// does not see the "no lint" suppression comment and mistakenly assume it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// RUN: not clang-tidy %s --checks='-*,google-explicit-constructor,google-readability-casting' 2>&1 | FileCheck %s
// RUN: not clang-tidy %s --checks='-*,google-explicit-constructor,modernize-avoid-c-style-cast' 2>&1 | FileCheck %s

// NOLINTBEGIN(google-explicit-constructor)
// NOLINTBEGIN(google-readability-casting)
// NOLINTBEGIN(modernize-avoid-c-style-cast)
class B { B(int i); };
auto Num2 = (unsigned int)(-1);
// NOLINTEND(google-explicit-constructor,google-readability-casting)
// NOLINTEND(google-explicit-constructor,modernize-avoid-c-style-cast)

// Note: the expected output has been split over several lines so that clang-tidy
// does not see the "no lint" suppression comment and mistakenly assume it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// RUN: not clang-tidy %s --checks='-*,google-explicit-constructor,google-readability-casting' 2>&1 | FileCheck %s
// RUN: not clang-tidy %s --checks='-*,google-explicit-constructor,modernize-avoid-c-style-cast' 2>&1 | FileCheck %s

// NOLINTBEGIN(google-explicit-constructor)
class A { A(int i); };
auto Num = (unsigned int)(-1);
// NOLINTEND(google-readability-casting)
// NOLINTEND(modernize-avoid-c-style-cast)

// Note: the expected output has been split over several lines so that clang-tidy
// does not see the "no lint" suppression comment and mistakenly assume it
Expand Down