Bug 1210871 - don't use reinterpret_cast in nsTHashtable.h; r=erahm
authorNathan Froyd <froydnj@mozilla.com>
Fri, 02 Oct 2015 20:32:15 -0400
changeset 265788 0b186b6c55592dc59e6710c8ce8fd9ff6d144bcd
parent 265787 bf1d7096324fc673df30784fef47e4da8979ba76
child 265833 03e8acfc181fea2620899600b0e58459151c7559
push id66031
push usernfroyd@mozilla.com
push dateFri, 02 Oct 2015 20:40:42 +0000
treeherdermozilla-inbound@0b186b6c5559 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1210871
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1210871 - don't use reinterpret_cast in nsTHashtable.h; r=erahm We don't need to reinterpret_cast when casting from void*, and we don't need to reinterpret_cast when we're casting up and down a class hierarchy. static_cast takes care of those cases just fine, and doesn't scare the reader into thinking that nsTHashtable is doing something unusual.
xpcom/glue/nsTHashtable.h
--- a/xpcom/glue/nsTHashtable.h
+++ b/xpcom/glue/nsTHashtable.h
@@ -6,22 +6,23 @@
 
 #ifndef nsTHashtable_h__
 #define nsTHashtable_h__
 
 #include "nscore.h"
 #include "PLDHashTable.h"
 #include "nsDebug.h"
 #include "mozilla/Assertions.h"
+#include "mozilla/Attributes.h"
+#include "mozilla/fallible.h"
 #include "mozilla/MemoryChecking.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Move.h"
-#include "mozilla/fallible.h"
 #include "mozilla/PodOperations.h"
-#include "mozilla/Attributes.h"
+#include "mozilla/TypeTraits.h"
 
 #include <new>
 
 /**
  * a base class for templated hashtables.
  *
  * Clients will rarely need to use this class directly. Check the derived
  * classes first, to see if they will meet your needs.
@@ -71,16 +72,18 @@
  * @see nsClassHashtable
  * @author "Benjamin Smedberg <bsmedberg@covad.net>"
  */
 
 template<class EntryType>
 class MOZ_NEEDS_NO_VTABLE_TYPE nsTHashtable
 {
   typedef mozilla::fallible_t fallible_t;
+  static_assert(mozilla::IsPointer<typename EntryType::KeyTypePointer>::value,
+                "KeyTypePointer should be a pointer");
 
 public:
   // Separate constructors instead of default aInitLength parameter since
   // otherwise the default no-arg constructor isn't found.
   nsTHashtable()
     : mTable(Ops(), sizeof(EntryType), PLDHashTable::kDefaultInitialLength)
   {}
   explicit nsTHashtable(uint32_t aInitLength)
@@ -353,37 +356,37 @@ nsTHashtable<EntryType>::Ops()
 }
 
 // static definitions
 
 template<class EntryType>
 PLDHashNumber
 nsTHashtable<EntryType>::s_HashKey(PLDHashTable* aTable, const void* aKey)
 {
-  return EntryType::HashKey(reinterpret_cast<const KeyTypePointer>(aKey));
+  return EntryType::HashKey(static_cast<const KeyTypePointer>(aKey));
 }
 
 template<class EntryType>
 bool
 nsTHashtable<EntryType>::s_MatchEntry(PLDHashTable* aTable,
                                       const PLDHashEntryHdr* aEntry,
                                       const void* aKey)
 {
   return ((const EntryType*)aEntry)->KeyEquals(
-    reinterpret_cast<const KeyTypePointer>(aKey));
+    static_cast<const KeyTypePointer>(aKey));
 }
 
 template<class EntryType>
 void
 nsTHashtable<EntryType>::s_CopyEntry(PLDHashTable* aTable,
                                      const PLDHashEntryHdr* aFrom,
                                      PLDHashEntryHdr* aTo)
 {
   EntryType* fromEntry =
-    const_cast<EntryType*>(reinterpret_cast<const EntryType*>(aFrom));
+    const_cast<EntryType*>(static_cast<const EntryType*>(aFrom));
 
   new (aTo) EntryType(mozilla::Move(*fromEntry));
 
   fromEntry->~EntryType();
 }
 
 template<class EntryType>
 void
@@ -393,17 +396,17 @@ nsTHashtable<EntryType>::s_ClearEntry(PL
   static_cast<EntryType*>(aEntry)->~EntryType();
 }
 
 template<class EntryType>
 void
 nsTHashtable<EntryType>::s_InitEntry(PLDHashEntryHdr* aEntry,
                                      const void* aKey)
 {
-  new (aEntry) EntryType(reinterpret_cast<KeyTypePointer>(aKey));
+  new (aEntry) EntryType(static_cast<KeyTypePointer>(aKey));
 }
 
 class nsCycleCollectionTraversalCallback;
 
 template<class EntryType>
 inline void
 ImplCycleCollectionUnlink(nsTHashtable<EntryType>& aField)
 {