[PATCH] D98416: [clang-tidy] Fix cppcoreguidelines-narrowing-conversions false positive

Carlos Galvez via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 15 08:08:06 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG4530c3bc4897: [clang-tidy] Fix cppcoreguidelines-narrowing-conversions false positive (authored by njames93, committed by carlosgalvezp).

Changed prior to commit:
  https://reviews.llvm.org/D98416?vs=329929&id=513912#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98416/new/

https://reviews.llvm.org/D98416

Files:
  clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/narrowing-conversions-narrowingfloatingpoint-option.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/narrowing-conversions-narrowingfloatingpoint-option.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/narrowing-conversions-narrowingfloatingpoint-option.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/narrowing-conversions-narrowingfloatingpoint-option.cpp
@@ -54,4 +54,11 @@
   f = __builtin_nan("0"); // double NaN is not narrowing.
 }
 
+double false_positive_const_qualified_cast(bool t) {
+  double b = 1.0;
+  constexpr double a = __builtin_huge_val();
+  // PR49498 The constness difference of 'a' and 'b' results in an implicit cast.
+  return t ? b : a;
+}
+
 } // namespace floats
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
+++ clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
@@ -510,6 +510,8 @@
   const BuiltinType *RhsType = getBuiltinType(Rhs);
   if (RhsType == nullptr || LhsType == nullptr)
     return;
+  if (LhsType == RhsType)
+    return;
   if (RhsType->getKind() == BuiltinType::Bool && LhsType->isSignedInteger())
     return handleBooleanToSignedIntegral(Context, SourceLoc, Lhs, Rhs);
   if (RhsType->isInteger() && LhsType->getKind() == BuiltinType::Bool)
@@ -549,6 +551,8 @@
   const Expr &Rhs = *Cast.getSubExpr();
   if (Lhs.isInstantiationDependent() || Rhs.isInstantiationDependent())
     return;
+  if (getBuiltinType(Lhs) == getBuiltinType(Rhs))
+    return;
   if (handleConditionalOperator(Context, Lhs, Rhs))
     return;
   SourceLocation SourceLoc = Lhs.getExprLoc();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98416.513912.patch
Type: text/x-patch
Size: 1805 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230415/6559eec5/attachment.bin>


More information about the cfe-commits mailing list