[clang-tools-extra] r348341 - Fix a false positive in misplaced-widening-cast

Jonas Toth via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 5 00:29:56 PST 2018


Author: jonastoth
Date: Wed Dec  5 00:29:56 2018
New Revision: 348341

URL: http://llvm.org/viewvc/llvm-project?rev=348341&view=rev
Log:
Fix a false positive in misplaced-widening-cast

Summary:
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.

Patch by dkrupp.

Reviewers: JonasToth, alexfh

Reviewed By: alexfh

Subscribers: rnkovacs, Szelethus, gamesh411, cfe-commits

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D55255

Modified:
    clang-tools-extra/trunk/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/bugprone-misplaced-widening-cast-explicit-only.cpp

Modified: clang-tools-extra/trunk/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp?rev=348341&r1=348340&r2=348341&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp Wed Dec  5 00:29:56 2018
@@ -213,8 +213,9 @@ void MisplacedWideningCastCheck::check(c
         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;
   }
 

Modified: clang-tools-extra/trunk/test/clang-tidy/bugprone-misplaced-widening-cast-explicit-only.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/bugprone-misplaced-widening-cast-explicit-only.cpp?rev=348341&r1=348340&r2=348341&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/bugprone-misplaced-widening-cast-explicit-only.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/bugprone-misplaced-widening-cast-explicit-only.cpp Wed Dec  5 00:29:56 2018
@@ -62,3 +62,21 @@ template <class> class A {
   enum Type {};
   static char *m_fn1() { char p = (Type)(&p - m_fn1()); }
 };
+
+enum DaysEnum {
+  MON,
+  TUE,
+  WED,
+  THR,
+  FRI,
+  SAT,
+  SUN
+};
+
+// Do not warn for int to enum casts.
+void nextDay(DaysEnum day) {
+  if (day < SUN)
+    day = (DaysEnum)(day + 1);
+  if (day < SUN)
+    day = static_cast<DaysEnum>(day + 1);
+}




More information about the cfe-commits mailing list