Skip to content

Reland "[clang-tidy] fix bugprone-narrowing-conversions false positive for conditional expression" #151874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,22 @@ bool NarrowingConversionsCheck::handleConditionalOperator(
// We have an expression like so: `output = cond ? lhs : rhs`
// From the point of view of narrowing conversion we treat it as two
// expressions `output = lhs` and `output = rhs`.
handleBinaryOperator(Context, CO->getLHS()->getExprLoc(), Lhs,
*CO->getLHS());
handleBinaryOperator(Context, CO->getRHS()->getExprLoc(), Lhs,
*CO->getRHS());
handleConditionalOperatorArgument(Context, Lhs, CO->getLHS());
handleConditionalOperatorArgument(Context, Lhs, CO->getRHS());
return true;
}
return false;
}

void NarrowingConversionsCheck::handleConditionalOperatorArgument(
const ASTContext &Context, const Expr &Lhs, const Expr *Arg) {
if (const auto *ICE = llvm::dyn_cast<ImplicitCastExpr>(Arg))
if (!Arg->getIntegerConstantExpr(Context))
Arg = ICE->getSubExpr();

handleBinaryOperator(Context, Arg->getExprLoc(), Lhs, *Arg);
}

void NarrowingConversionsCheck::handleImplicitCast(
const ASTContext &Context, const ImplicitCastExpr &Cast) {
if (Cast.getExprLoc().isMacroID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class NarrowingConversionsCheck : public ClangTidyCheck {
bool handleConditionalOperator(const ASTContext &Context, const Expr &Lhs,
const Expr &Rhs);

void handleConditionalOperatorArgument(const ASTContext &Context,
const Expr &Lhs, const Expr *Arg);
void handleImplicitCast(const ASTContext &Context,
const ImplicitCastExpr &Cast);

Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ Changes in existing checks
<clang-tidy/checks/bugprone/infinite-loop>` check by adding detection for
variables introduced by structured bindings.

- Improved :doc:`bugprone-narrowing-conversions
<clang-tidy/checks/bugprone/narrowing-conversions>` check by fixing
false positive from analysis of a conditional expression in C.

- Improved :doc:`bugprone-reserved-identifier
<clang-tidy/checks/bugprone/reserved-identifier>` check by ignoring
declarations in system headers.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %check_clang_tidy %s bugprone-narrowing-conversions %t -- \
// RUN: -- -target x86_64-unknown-linux

char test_char(int cond, char c) {
char ret = cond > 0 ? ':' : c;
return ret;
}

short test_short(int cond, short s) {
short ret = cond > 0 ? ':' : s;
return ret;
}

int test_int(int cond, int i) {
int ret = cond > 0 ? ':' : i;
return ret;
}

void test(int cond, int i) {
char x = cond > 0 ? ':' : i;
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: narrowing conversion from 'int' to signed type 'char' is implementation-defined [bugprone-narrowing-conversions]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %check_clang_tidy %s bugprone-narrowing-conversions %t -- \
// RUN: -- -target x86_64-unknown-linux

char test_char(int cond, char c) {
char ret = cond > 0 ? ':' : c;
return ret;
}

short test_short(int cond, short s) {
short ret = cond > 0 ? ':' : s;
return ret;
}

int test_int(int cond, int i) {
int ret = cond > 0 ? ':' : i;
return ret;
}

void test(int cond, int i) {
char x = cond > 0 ? ':' : i;
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: narrowing conversion from 'int' to signed type 'char' is implementation-defined [bugprone-narrowing-conversions]
}