Skip to content

Commit 53ff132

Browse files
committed
[clang-tidy] fix false positive for implicit conversion of comparison result in C23
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 c0c36aa commit 53ff132

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ AST_MATCHER(Stmt, isMacroExpansion) {
2626
return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc);
2727
}
2828

29+
AST_MATCHER(Stmt, isC23) { return Finder->getASTContext().getLangOpts().C23; }
30+
2931
bool isNULLMacroExpansion(const Stmt *Statement, ASTContext &Context) {
3032
SourceManager &SM = Context.getSourceManager();
3133
const LangOptions &LO = Context.getLangOpts();
@@ -298,6 +300,11 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
298300
hasCastKind(CK_FloatingToBoolean),
299301
hasCastKind(CK_PointerToBoolean),
300302
hasCastKind(CK_MemberPointerToBoolean)),
303+
// Exclude cases of C23 comparison result.
304+
unless(allOf(
305+
isC23(),
306+
hasSourceExpression(binaryOperator(hasAnyOperatorName(
307+
">", ">=", "==", "!=", "<", "<="))))),
301308
// Exclude case of using if or while statements with variable
302309
// declaration, e.g.:
303310
// if (int var = functionCall()) {}

clang-tools-extra/docs/ReleaseNotes.rst

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

249250
- Improved :doc:`readability-redundant-smartptr-get
250251
<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)