[PATCH] D55255: Fix a false positive in misplaced-widening-cast

Daniel Krupp via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 4 00:40:40 PST 2018


dkrupp created this revision.
dkrupp added reviewers: JonasToth, alexfh.
dkrupp added a project: clang-tools-extra.
Herald added subscribers: cfe-commits, gamesh411, Szelethus, rnkovacs.

bugprone-misplaced-widening-cast check
used to give a false warning to the
following example.

      

enum DaysEnum{

  MON = 0,
  TUE = 1
  };


day = (DaysEnum)(day + 1);
 //warning: either cast from 'int' to 'DaysEnum' is ineffective...

But i think int to enum cast is not widening neither ineffective.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D55255

Files:
  clang-tidy/bugprone/MisplacedWideningCastCheck.cpp
  test/clang-tidy/bugprone-misplaced-widening-cast-explicit-only.cpp


Index: test/clang-tidy/bugprone-misplaced-widening-cast-explicit-only.cpp
===================================================================
--- test/clang-tidy/bugprone-misplaced-widening-cast-explicit-only.cpp
+++ test/clang-tidy/bugprone-misplaced-widening-cast-explicit-only.cpp
@@ -62,3 +62,29 @@
   enum Type {};
   static char *m_fn1() { char p = (Type)(&p - m_fn1()); }
 };
+
+enum DaysEnum{
+MON = 0,
+TUE = 1,
+WED = 2,
+THR = 3,
+FRI = 4,
+SAT = 5,
+SUN = 6
+};
+
+//do not warn for int to enum casts
+DaysEnum nextDay(DaysEnum day){
+  if (day < SUN)
+      day = (DaysEnum)(day + 1);
+  return day;
+}
+
+//do not warn for int to enum casts
+DaysEnum nextDay_cpp(DaysEnum day){
+  if (day < SUN)
+      day = static_cast<DaysEnum>(day + 1);
+  return day;
+}
+
+
Index: clang-tidy/bugprone/MisplacedWideningCastCheck.cpp
===================================================================
--- clang-tidy/bugprone/MisplacedWideningCastCheck.cpp
+++ clang-tidy/bugprone/MisplacedWideningCastCheck.cpp
@@ -213,8 +213,9 @@
         dyn_cast<BuiltinType>(CastType->getUnqualifiedDesugaredType());
     const auto *CalcBuiltinType =
         dyn_cast<BuiltinType>(CalcType->getUnqualifiedDesugaredType());
-    if (CastBuiltinType && CalcBuiltinType &&
-        !isFirstWider(CastBuiltinType->getKind(), CalcBuiltinType->getKind()))
+    if (!CastBuiltinType || !CalcBuiltinType)
+      return;
+    if (!isFirstWider(CastBuiltinType->getKind(), CalcBuiltinType->getKind()))
       return;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55255.176557.patch
Type: text/x-patch
Size: 1505 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181204/536860db/attachment.bin>


More information about the cfe-commits mailing list