Skip to content

Commit a1d31ca

Browse files
authored
[clang-tidy] fix false positive for implicit conversion of comparison result in C23 (#113639)
Fixed #111013 bool will be builtin type in C23 but comparison result in C is still int. It is no need to change this kind of implicit cast to explicit cast.
1 parent 5aa1275 commit a1d31ca

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../utils/FixItHintUtils.h"
1111
#include "clang/AST/ASTContext.h"
1212
#include "clang/ASTMatchers/ASTMatchFinder.h"
13+
#include "clang/ASTMatchers/ASTMatchers.h"
1314
#include "clang/Lex/Lexer.h"
1415
#include "clang/Tooling/FixIt.h"
1516
#include <queue>
@@ -26,6 +27,8 @@ AST_MATCHER(Stmt, isMacroExpansion) {
2627
return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc);
2728
}
2829

30+
AST_MATCHER(Stmt, isC23) { return Finder->getASTContext().getLangOpts().C23; }
31+
2932
bool isNULLMacroExpansion(const Stmt *Statement, ASTContext &Context) {
3033
SourceManager &SM = Context.getSourceManager();
3134
const LangOptions &LO = Context.getLangOpts();
@@ -298,6 +301,11 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
298301
hasCastKind(CK_FloatingToBoolean),
299302
hasCastKind(CK_PointerToBoolean),
300303
hasCastKind(CK_MemberPointerToBoolean)),
304+
// Exclude cases of C23 comparison result.
305+
unless(allOf(isC23(),
306+
hasSourceExpression(ignoringParens(
307+
binaryOperator(hasAnyOperatorName(
308+
">", ">=", "==", "!=", "<", "<=")))))),
301309
// Exclude case of using if or while statements with variable
302310
// declaration, e.g.:
303311
// if (int var = functionCall()) {}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ Changes in existing checks
249249
- Improved :doc:`readability-implicit-bool-conversion
250250
<clang-tidy/checks/readability/implicit-bool-conversion>` check
251251
by adding the option `UseUpperCaseLiteralSuffix` to select the
252-
case of the literal suffix in fixes.
252+
case of the literal suffix in fixes and fixing false positive for implicit
253+
conversion of comparison result in C23.
253254

254255
- Improved :doc:`readability-redundant-smartptr-get
255256
<clang-tidy/checks/readability/redundant-smartptr-get>` check to

clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() {
304304
// CHECK-FIXES: functionTakingBool((-0.0) != 0.0);
305305
}
306306

307+
void ignoreImplicitCastToBoolForComparisonResult() {
308+
bool boolFromComparison0 = 1 != 0;
309+
bool boolFromComparison1 = 1 == 0;
310+
bool boolFromComparison2 = 1 > 0;
311+
bool boolFromComparison3 = 1 >= 0;
312+
bool boolFromComparison4 = 1 < 0;
313+
bool boolFromComparison5 = 1 <= 0;
314+
}
315+
307316
void ignoreExplicitCastsToBool() {
308317
int integer = 10;
309318
bool boolComingFromInt = (bool)integer;

0 commit comments

Comments
 (0)