[all-commits] [llvm/llvm-project] c3902e: [clang-tidy] fix `bugprone-narrowing-conversions` ...

Andrey via All-commits all-commits at lists.llvm.org
Sun Aug 3 03:40:00 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: c3902e46b748cd808fbffeea25489bb41b15a6b2
      https://github.com/llvm/llvm-project/commit/c3902e46b748cd808fbffeea25489bb41b15a6b2
  Author: Andrey <andrey.a.davydov at gmail.com>
  Date:   2025-08-03 (Sun, 03 Aug 2025)

  Changed paths:
    M clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
    M clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.h
    M clang-tools-extra/docs/ReleaseNotes.rst
    A clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-conditional-expressions.c
    A clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-conditional-expressions.cpp

  Log Message:
  -----------
  [clang-tidy] fix `bugprone-narrowing-conversions` false positive for conditional expression (#139474)

Let's consider the following code from the issue #139467:
```c
void test(int cond, char c) {
    char ret = cond > 0 ? ':' : c;
}
```
Initializer of `ret` looks the following:
```
-ImplicitCastExpr 'char' <IntegralCast>
 `-ConditionalOperator 'int'
   |-BinaryOperator 'int' '>'
   | |-ImplicitCastExpr 'int' <LValueToRValue>
   | | `-DeclRefExpr 'int' lvalue ParmVar 'cond' 'int'
   | `-IntegerLiteral 'int' 0
   |-CharacterLiteral 'int' 58
   `-ImplicitCastExpr 'int' <IntegralCast>
     `-ImplicitCastExpr 'char' <LValueToRValue>
       `-DeclRefExpr 'char' lvalue ParmVar 'c' 'char'
```
So it could be seen that `RHS` of the conditional operator is
`DeclRefExpr 'c'` which is casted to `int` and then the whole
conditional expression is casted to 'char'. But this last conversion is
not narrowing, because `RHS` was `char` _initially_. We should just
remove the cast from `char` to `int` before the narrowing conversion
check.

Fixes #139467



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list