Skip to content

Conversation

@ckandeler
Copy link
Member

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Oct 28, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Christian Kandeler (ckandeler)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/165411.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp (+6)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp (+4-2)
diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp index 174ecb0ed7b77..80c53de0cf237 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -269,6 +269,12 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { return; } break; + case CK_BaseToDerived: + if (!needsConstCast(SourceType, DestType)) { + ReplaceWithNamedCast("static_cast"); + return; + } + break; default: break; } diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp index 7ccdf705e8399..f9feb8854249b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp @@ -102,9 +102,11 @@ void f(int a, double b, const char *cpc, const void *cpv, X *pX) { // CHECK-FIXES: b1 = static_cast<int>(b); Y *pB = (Y*)pX; - // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [ + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast {{.*}} + // CHECK-FIXES: Y *pB = static_cast<Y*>(pX); Y &rB = (Y&)*pX; - // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [ + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast {{.*}} + // CHECK-FIXES: Y &rB = static_cast<Y&>(*pX); const char *pc3 = (const char*)cpv; // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}; use static_cast [ 
@ckandeler ckandeler requested a review from localspook October 28, 2025 15:22
Comment on lines +272 to +277
case CK_BaseToDerived:
if (!needsConstCast(SourceType, DestType)) {
ReplaceWithNamedCast("static_cast");
return;
}
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it a best practice to use static_cast for base to derived always?
I think under RTTI, we should use dynamic_cast and under non-RTTI, static_cast could be used also.

Copy link
Member Author

Choose a reason for hiding this comment

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

The fact that the code uses an unchecked cast implies a priori knowledge about the safety to do so, so static_cast seems to be the correct equivalent. Using dynamic_cast would likely incur unwanted overhead.
(I'm not too familiar with the clang-tidy code base: Is it possible/recommended to suggest competing fixes? If so, what happens to the respective code if --fix is used?)

Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK under the hood C-style cast tries first to static_cast then reinterpret_cast so we transform code equally.

(I'm not too familiar with the clang-tidy code base: Is it possible/recommended to suggest competing fixes? If so, what happens to the respective code if --fix is used?)

I we don't have competing fixes by design, but there is only some conflict resolution in case two checks try to change one piece of code: https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-tidy/ClangTidy.cpp#L149-L175

It's not recommended competing fixes - it's just better to write in warning message possible options and let the dev write by hand

@EugeneZelenko
Copy link
Contributor

Please update Release Notes.

@ckandeler ckandeler force-pushed the QTCREATORBUG-18270-clang-tidy-fixits branch from 2240190 to 60c1beb Compare October 29, 2025 17:52
@ckandeler
Copy link
Member Author

ping


- Improved :doc:`google-readability-casting
<clang-tidy/checks/google/readability-casting>` check by adding a fix-it
for downcasts.
Copy link
Contributor

Choose a reason for hiding this comment

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

notes for for downcasts?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I don't understand what you mean. Can you elaborate?

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant using fix-it notes in this statement.

Copy link
Member Author

Choose a reason for hiding this comment

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

Like this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes.

@ckandeler ckandeler force-pushed the QTCREATORBUG-18270-clang-tidy-fixits branch from 60c1beb to 2920d95 Compare November 7, 2025 16:20
Copy link
Contributor

@vbvictor vbvictor left a comment

Choose a reason for hiding this comment

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

LGTM

@ckandeler ckandeler merged commit da9015a into llvm:main Nov 12, 2025
12 checks passed
@ckandeler ckandeler deleted the QTCREATORBUG-18270-clang-tidy-fixits branch November 12, 2025 07:38
@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 12, 2025

LLVM Buildbot has detected a new failure on builder sanitizer-x86_64-linux-fast running on sanitizer-buildbot3 while building clang-tools-extra at step 2 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/169/builds/16973

Here is the relevant piece of the build log for the reference
Step 2 (annotate) failure: 'python ../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py' (failure) ... llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using lld-link: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/lld-link llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld64.lld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/wasm-ld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using ld.lld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld.lld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using lld-link: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/lld-link llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld64.lld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/wasm-ld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/main.py:74: note: The test suite configuration requested an individual test timeout of 0 seconds but a timeout of 900 seconds was requested on the command line. Forcing timeout to be 900 seconds. -- Testing: 93715 tests, 64 workers -- Testing: 0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. FAIL: Clang :: Analysis/ftime-trace.cpp (30077 of 93715) ******************** TEST 'Clang :: Analysis/ftime-trace.cpp' FAILED ******************** Exit Code: 1 Command Output (stdout): -- # RUN: at line 1 /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/lib/clang/22/include -nostdsysteminc -analyze -setup-static-analyzer -analyzer-checker=core /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp -ftime-trace=/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.raw.json -ftime-trace-granularity=0 -verify # executed command: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/lib/clang/22/include -nostdsysteminc -analyze -setup-static-analyzer -analyzer-checker=core /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp -ftime-trace=/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.raw.json -ftime-trace-granularity=0 -verify # note: command had no output on stdout or stderr # RUN: at line 2 "/usr/bin/python3" -c 'import json, sys; print(json.dumps(json.load(sys.stdin), indent=4))' < /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.raw.json > /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json # executed command: /usr/bin/python3 -c 'import json, sys; print(json.dumps(json.load(sys.stdin), indent=4))' # note: command had no output on stdout or stderr # RUN: at line 3 /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/FileCheck --input-file=/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json --check-prefix=CHECK /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp # executed command: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/FileCheck --input-file=/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json --check-prefix=CHECK /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp # .---command stderr------------ # | /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp:34:11: error: CHECK: expected string not found in input # | // CHECK: "name": "Total CheckerManager::runCheckersForStmt (Pre)", # | ^ # | /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json:2602:3: note: scanning from here # | } # | ^ # | /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json:2730:2: note: possible intended match here # | "name": "Total CheckerManager::runCheckersForStmt (Post)", # | ^ # | # | Input file: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json # | Check file: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp # | # | -dump-input=help explains the following input dump. # | # | Input was: # | <<<<<< # | . # | . # | . # | 2597: "dur": 4112, Step 10 (stage2/asan_ubsan check) failure: stage2/asan_ubsan check (failure) ... llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using lld-link: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/lld-link llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld64.lld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/wasm-ld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using ld.lld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld.lld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using lld-link: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/lld-link llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld64.lld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:531: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/wasm-ld llvm-lit: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/main.py:74: note: The test suite configuration requested an individual test timeout of 0 seconds but a timeout of 900 seconds was requested on the command line. Forcing timeout to be 900 seconds. -- Testing: 93715 tests, 64 workers -- Testing: 0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. FAIL: Clang :: Analysis/ftime-trace.cpp (30077 of 93715) ******************** TEST 'Clang :: Analysis/ftime-trace.cpp' FAILED ******************** Exit Code: 1 Command Output (stdout): -- # RUN: at line 1 /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/lib/clang/22/include -nostdsysteminc -analyze -setup-static-analyzer -analyzer-checker=core /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp -ftime-trace=/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.raw.json -ftime-trace-granularity=0 -verify # executed command: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/clang -cc1 -internal-isystem /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/lib/clang/22/include -nostdsysteminc -analyze -setup-static-analyzer -analyzer-checker=core /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp -ftime-trace=/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.raw.json -ftime-trace-granularity=0 -verify # note: command had no output on stdout or stderr # RUN: at line 2 "/usr/bin/python3" -c 'import json, sys; print(json.dumps(json.load(sys.stdin), indent=4))' < /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.raw.json > /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json # executed command: /usr/bin/python3 -c 'import json, sys; print(json.dumps(json.load(sys.stdin), indent=4))' # note: command had no output on stdout or stderr # RUN: at line 3 /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/FileCheck --input-file=/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json --check-prefix=CHECK /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp # executed command: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/FileCheck --input-file=/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json --check-prefix=CHECK /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp # .---command stderr------------ # | /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp:34:11: error: CHECK: expected string not found in input # | // CHECK: "name": "Total CheckerManager::runCheckersForStmt (Pre)", # | ^ # | /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json:2602:3: note: scanning from here # | } # | ^ # | /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json:2730:2: note: possible intended match here # | "name": "Total CheckerManager::runCheckersForStmt (Post)", # | ^ # | # | Input file: /home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/tools/clang/test/Analysis/Output/ftime-trace.cpp.tmp.formatted.json # | Check file: /home/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/Analysis/ftime-trace.cpp # | # | -dump-input=help explains the following input dump. # | # | Input was: # | <<<<<< # | . # | . # | . # | 2597: "dur": 4112, 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment