Author: Congcong Cai Date: 2024-10-28T11:36:37+08:00 New Revision: a1d31caa8c53082d12f580122dcf2b2ff8285e78
URL: https://github.com/llvm/llvm-project/commit/a1d31caa8c53082d12f580122dcf2b2ff8285e78 DIFF: https://github.com/llvm/llvm-project/commit/a1d31caa8c53082d12f580122dcf2b2ff8285e78.diff LOG: [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. Added: Modified: clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 968a4a55a6d798..f9fd1d903e231e 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -10,6 +10,7 @@ #include "../utils/FixItHintUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Lex/Lexer.h" #include "clang/Tooling/FixIt.h" #include <queue> @@ -26,6 +27,8 @@ AST_MATCHER(Stmt, isMacroExpansion) { return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc); } +AST_MATCHER(Stmt, isC23) { return Finder->getASTContext().getLangOpts().C23; } + bool isNULLMacroExpansion(const Stmt *Statement, ASTContext &Context) { SourceManager &SM = Context.getSourceManager(); const LangOptions &LO = Context.getLangOpts(); @@ -298,6 +301,11 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) { hasCastKind(CK_FloatingToBoolean), hasCastKind(CK_PointerToBoolean), hasCastKind(CK_MemberPointerToBoolean)), + // Exclude cases of C23 comparison result. + unless(allOf(isC23(), + hasSourceExpression(ignoringParens( + binaryOperator(hasAnyOperatorName( + ">", ">=", "==", "!=", "<", "<=")))))), // Exclude case of using if or while statements with variable // declaration, e.g.: // if (int var = functionCall()) {} diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 876689c40fcdb2..4cc4c2146d7e33 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -249,7 +249,8 @@ Changes in existing checks - Improved :doc:`readability-implicit-bool-conversion <clang-tidy/checks/readability/implicit-bool-conversion>` check by adding the option `UseUpperCaseLiteralSuffix` to select the - case of the literal suffix in fixes. + case of the literal suffix in fixes and fixing false positive for implicit + conversion of comparison result in C23. - Improved :doc:`readability-redundant-smartptr-get <clang-tidy/checks/readability/redundant-smartptr-get>` check to diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c index f3dc32c10d640a..0b231d10adf8fc 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c @@ -304,6 +304,15 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() { // CHECK-FIXES: functionTakingBool((-0.0) != 0.0); } +void ignoreImplicitCastToBoolForComparisonResult() { + bool boolFromComparison0 = 1 != 0; + bool boolFromComparison1 = 1 == 0; + bool boolFromComparison2 = 1 > 0; + bool boolFromComparison3 = 1 >= 0; + bool boolFromComparison4 = 1 < 0; + bool boolFromComparison5 = 1 <= 0; +} + void ignoreExplicitCastsToBool() { int integer = 10; bool boolComingFromInt = (bool)integer; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits