diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp index bb015abd3d86..e1161726f1ad 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -78,6 +78,10 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { return; } + // The rest of this check is only relevant to C++. + if (!Result.Context->getLangOpts().CPlusPlus) + return; + std::string DestTypeString = CastExpr->getTypeAsWritten().getAsString(); auto diag_builder = diff --git a/clang-tools-extra/test/clang-tidy/avoid-c-style-casts.c b/clang-tools-extra/test/clang-tidy/avoid-c-style-casts.c new file mode 100644 index 000000000000..2bdad179a739 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/avoid-c-style-casts.c @@ -0,0 +1,9 @@ +// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s google-readability-casting %t -x c +// REQUIRES: shell + +void f(const char *cpc) { + const char *cpc2 = (const char*)cpc; + // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: Redundant cast to the same type. [google-readability-casting] + // CHECK-FIXES: const char *cpc2 = cpc; + char *pc = (char*)cpc; +} diff --git a/clang-tools-extra/test/clang-tidy/check_clang_tidy_fix.sh b/clang-tools-extra/test/clang-tidy/check_clang_tidy_fix.sh index 05c1eaec12d9..4cdadeffaf41 100755 --- a/clang-tools-extra/test/clang-tidy/check_clang_tidy_fix.sh +++ b/clang-tools-extra/test/clang-tidy/check_clang_tidy_fix.sh @@ -14,6 +14,10 @@ CHECK_TO_RUN=$2 TEMPORARY_FILE=$3.cpp # Feed the rest arguments to clang-tidy after --. shift 3 +CLANG_TIDY_ARGS=--std=c++11 +if (($# > 0)) ; then + CLANG_TIDY_ARGS=$* +fi set -o errexit @@ -23,7 +27,7 @@ set -o errexit sed 's#// *CHECK-[A-Z-]*:.*#//#' ${INPUT_FILE} > ${TEMPORARY_FILE} clang-tidy ${TEMPORARY_FILE} -fix --checks="-*,${CHECK_TO_RUN}" \ - -- --std=c++11 $* > ${TEMPORARY_FILE}.msg 2>&1 + -- ${CLANG_TIDY_ARGS} > ${TEMPORARY_FILE}.msg 2>&1 FileCheck -input-file=${TEMPORARY_FILE} ${INPUT_FILE} \ -check-prefix=CHECK-FIXES -strict-whitespace