Bug 1344629 - Part 6.5: Allow get() on nsLiteralStrings, excluding temporaries. r=dbaron
authorDavid Major <dmajor@mozilla.com>
Tue, 14 Mar 2017 15:26:36 +1300
changeset 347456 c3433c49b31df89338e43b660e4e1d1f1d9c8083
parent 347455 ac66fac84e17d414860f495cffe7e3ffef70cac7
child 347457 06557c34d4e5be278771539c857640752e164f14
push id31496
push usercbook@mozilla.com
push dateTue, 14 Mar 2017 13:21:57 +0000
treeherdermozilla-central@9a26ed658fdc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdbaron
bugs1344629, 1341513
milestone55.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 1344629 - Part 6.5: Allow get() on nsLiteralStrings, excluding temporaries. r=dbaron This prevents the antipattern found in bug 1341513. MozReview-Commit-ID: JhxgC7aQDUA
xpcom/string/nsTLiteralString.h
--- a/xpcom/string/nsTLiteralString.h
+++ b/xpcom/string/nsTLiteralString.h
@@ -28,14 +28,29 @@ public:
    */
 
   template<size_type N>
   explicit nsTLiteralString_CharT(const char_type (&aStr)[N])
     : string_type(const_cast<char_type*>(aStr), N - 1, F_TERMINATED | F_LITERAL)
   {
   }
 
+  /**
+   * Prohibit get() on temporaries as in nsLiteralCString("x").get().
+   * These should be written as just "x", using a string literal directly.
+   */
+#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
+  char16ptr_t get() const && = delete;
+  char16ptr_t get() const &
+#else
+  const char_type* get() const && = delete;
+  const char_type* get() const &
+#endif
+  {
+    return mData;
+  }
+
 private:
 
   // NOT TO BE IMPLEMENTED
   template<size_type N>
   nsTLiteralString_CharT(char_type (&aStr)[N]) = delete;
 };