Bug 1142503 - don't use QueryInterface when the compiler can do the cast for us; r=ehsan
authorNathan Froyd <froydnj@mozilla.com>
Thu, 12 Mar 2015 09:43:50 -0400
changeset 233559 27533222749d6f1787dcf4b385285287338a1b6f
parent 233558 7e4e5e971d95f0ecfe2c721b6db0743b6ba7076b
child 233560 af8b6c7b05f8a3ce7898a83d788a080b0c0466de
push id28417
push userryanvm@gmail.com
push dateFri, 13 Mar 2015 19:52:44 +0000
treeherdermozilla-central@977add19414a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1142503
milestone39.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 1142503 - don't use QueryInterface when the compiler can do the cast for us; r=ehsan Calling QueryInterface with a statically known IID should typically not be necessary. In those cases where it's not, the compiler can do the cast for us, though we have to supply the reference-counting that QueryInterface would do. In passing, several redundant null-checks for the result of |new T| have been deleted.
editor/composer/nsComposerCommandsUpdater.cpp
editor/libeditor/nsHTMLURIRefObject.cpp
editor/txtsvc/nsTextServicesDocument.cpp
embedding/components/commandhandler/nsCommandGroup.cpp
embedding/components/find/nsWebBrowserFind.cpp
intl/strres/nsStringBundle.cpp
security/manager/ssl/src/PSMContentListener.cpp
toolkit/components/places/nsNavHistoryResult.cpp
widget/nsPrintSettingsImpl.cpp
widget/windows/nsImageClipboard.cpp
widget/windows/nsPrintSettingsWin.cpp
xpcom/io/nsAppFileLocationProvider.cpp
xpfe/appshell/nsWindowMediator.cpp
--- a/editor/composer/nsComposerCommandsUpdater.cpp
+++ b/editor/composer/nsComposerCommandsUpdater.cpp
@@ -380,14 +380,12 @@ nsComposerCommandsUpdater::Notify(nsITim
 #if 0
 #pragma mark -
 #endif
 
 
 nsresult
 NS_NewComposerCommandsUpdater(nsISelectionListener** aInstancePtrResult)
 {
-  nsComposerCommandsUpdater* newThang = new nsComposerCommandsUpdater;
-  NS_ENSURE_TRUE(newThang, NS_ERROR_OUT_OF_MEMORY);
-
-  return newThang->QueryInterface(NS_GET_IID(nsISelectionListener),
-                                  (void **)aInstancePtrResult);
+  nsRefPtr<nsComposerCommandsUpdater> newThang = new nsComposerCommandsUpdater;
+  newThang.forget(aInstancePtrResult);
+  return NS_OK;
 }
--- a/editor/libeditor/nsHTMLURIRefObject.cpp
+++ b/editor/libeditor/nsHTMLURIRefObject.cpp
@@ -249,12 +249,12 @@ nsHTMLURIRefObject::SetNode(nsIDOMNode *
 nsresult NS_NewHTMLURIRefObject(nsIURIRefObject** aResult, nsIDOMNode* aNode)
 {
   nsRefPtr<nsHTMLURIRefObject> refObject = new nsHTMLURIRefObject();
   nsresult rv = refObject->SetNode(aNode);
   if (NS_FAILED(rv)) {
     *aResult = 0;
     return rv;
   }
-  return refObject->QueryInterface(NS_GET_IID(nsIURIRefObject),
-                                   (void**)aResult);
+  refObject.forget(aResult);
+  return NS_OK;
 }
 
--- a/editor/txtsvc/nsTextServicesDocument.cpp
+++ b/editor/txtsvc/nsTextServicesDocument.cpp
@@ -1995,31 +1995,31 @@ nsTextServicesDocument::GetDocumentConte
     nsCOMPtr<nsIDOMHTMLElement> bodyElement;
 
     result = htmlDoc->GetBody(getter_AddRefs(bodyElement));
 
     NS_ENSURE_SUCCESS(result, result);
 
     NS_ENSURE_TRUE(bodyElement, NS_ERROR_FAILURE);
 
-    result = bodyElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void **)aNode);
+    bodyElement.forget(aNode);
   }
   else
   {
     // For non-HTML documents, the content root node will be the document element.
 
     nsCOMPtr<nsIDOMElement> docElement;
 
     result = mDOMDocument->GetDocumentElement(getter_AddRefs(docElement));
 
     NS_ENSURE_SUCCESS(result, result);
 
     NS_ENSURE_TRUE(docElement, NS_ERROR_FAILURE);
 
-    result = docElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void **)aNode);
+    docElement.forget(aNode);
   }
 
   return result;
 }
 
 nsresult
 nsTextServicesDocument::CreateDocumentContentRange(nsRange** aRange)
 {
--- a/embedding/components/commandhandler/nsCommandGroup.cpp
+++ b/embedding/components/commandhandler/nsCommandGroup.cpp
@@ -283,30 +283,30 @@ nsControllerCommandGroup::IsCommandInGro
   }
   return NS_OK;
 }
 
 /* nsISimpleEnumerator getGroupsEnumerator (); */
 NS_IMETHODIMP
 nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator **_retval)
 {
-  nsGroupsEnumerator *groupsEnum = new nsGroupsEnumerator(mGroupsHash);
-  if (!groupsEnum) return NS_ERROR_OUT_OF_MEMORY;
+  nsRefPtr<nsGroupsEnumerator> groupsEnum = new nsGroupsEnumerator(mGroupsHash);
 
-  return groupsEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval);
+  groupsEnum.forget(_retval);
+  return NS_OK;
 }
 
 /* nsISimpleEnumerator getEnumeratorForGroup (in DOMString aGroup); */
 NS_IMETHODIMP
 nsControllerCommandGroup::GetEnumeratorForGroup(const char *aGroup, nsISimpleEnumerator **_retval)
 {
   nsDependentCString groupKey(aGroup);
   nsTArray<nsCString> *commandList = mGroupsHash.Get(groupKey); // may be null
 
-  nsNamedGroupEnumerator *theGroupEnum = new nsNamedGroupEnumerator(commandList);
-  if (!theGroupEnum) return NS_ERROR_OUT_OF_MEMORY;
+  nsRefPtr<nsNamedGroupEnumerator> theGroupEnum = new nsNamedGroupEnumerator(commandList);
 
-  return theGroupEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval);
+  theGroupEnum.forget(_retval);
+  return NS_OK;
 }
 
 #if 0
 #pragma mark -
 #endif
--- a/embedding/components/find/nsWebBrowserFind.cpp
+++ b/embedding/components/find/nsWebBrowserFind.cpp
@@ -404,26 +404,27 @@ nsresult nsWebBrowserFind::GetRootNode(n
   nsCOMPtr<nsIDOMHTMLDocument> htmlDoc = do_QueryInterface(aDomDoc);
   if (htmlDoc)
   {
     // For HTML documents, the content root node is the body.
     nsCOMPtr<nsIDOMHTMLElement> bodyElement;
     rv = htmlDoc->GetBody(getter_AddRefs(bodyElement));
     NS_ENSURE_SUCCESS(rv, rv);
     NS_ENSURE_ARG_POINTER(bodyElement);
-    return bodyElement->QueryInterface(NS_GET_IID(nsIDOMNode),
-                                       (void **)aNode);
+    bodyElement.forget(aNode);
+    return NS_OK;
   }
 
   // For non-HTML documents, the content root node will be the doc element.
   nsCOMPtr<nsIDOMElement> docElement;
   rv = aDomDoc->GetDocumentElement(getter_AddRefs(docElement));
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_ARG_POINTER(docElement);
-  return docElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void **)aNode);
+  docElement.forget(aNode);
+  return NS_OK;
 }
 
 nsresult nsWebBrowserFind::SetRangeAroundDocument(nsIDOMRange* aSearchRange,
                                                   nsIDOMRange* aStartPt,
                                                   nsIDOMRange* aEndPt,
                                                   nsIDOMDocument* aDoc)
 {
     nsCOMPtr<nsIDOMNode> bodyNode;
--- a/intl/strres/nsStringBundle.cpp
+++ b/intl/strres/nsStringBundle.cpp
@@ -646,19 +646,18 @@ nsStringBundleService::CreateExtensibleB
 
   nsRefPtr<nsExtensibleStringBundle> bundle = new nsExtensibleStringBundle();
 
   nsresult res = bundle->Init(aCategory, this);
   if (NS_FAILED(res)) {
     return res;
   }
 
-  res = bundle->QueryInterface(NS_GET_IID(nsIStringBundle), (void**) aResult);
-
-  return res;
+  bundle.forget(aResult);
+  return NS_OK;
 }
 
 #define GLOBAL_PROPERTIES "chrome://global/locale/global-strres.properties"
 
 nsresult
 nsStringBundleService::FormatWithBundle(nsIStringBundle* bundle, nsresult aStatus,
                                         uint32_t argCount, char16_t** argArray,
                                         char16_t* *result)
--- a/security/manager/ssl/src/PSMContentListener.cpp
+++ b/security/manager/ssl/src/PSMContentListener.cpp
@@ -265,27 +265,23 @@ PSMContentListener::CanHandleContent(con
 
 NS_IMETHODIMP
 PSMContentListener::DoContent(const nsACString & aContentType,
                                bool aIsContentPreferred,
                                nsIRequest * aRequest,
                                nsIStreamListener ** aContentHandler,
                                bool * aAbortProcess)
 {
-  PSMContentDownloader *downLoader;
   uint32_t type;
   type = getPSMContentType(PromiseFlatCString(aContentType).get());
   PR_LOG(gPIPNSSLog, PR_LOG_DEBUG, ("PSMContentListener::DoContent\n"));
   if (type != PSMContentDownloader::UNKNOWN_TYPE) {
-    downLoader = new PSMContentDownloader(type);
-    if (downLoader) {
-      downLoader->QueryInterface(NS_GET_IID(nsIStreamListener), 
-                                            (void **)aContentHandler);
-      return NS_OK;
-    }
+    nsRefPtr<PSMContentDownloader> downLoader = new PSMContentDownloader(type);
+    downLoader.forget(aContentHandler);
+    return NS_OK;
   }
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 PSMContentListener::GetLoadCookie(nsISupports * *aLoadCookie)
 {
   *aLoadCookie = mLoadCookie;
--- a/toolkit/components/places/nsNavHistoryResult.cpp
+++ b/toolkit/components/places/nsNavHistoryResult.cpp
@@ -4318,18 +4318,19 @@ nsNavHistoryResult::SetSuppressNotificat
 NS_IMETHODIMP
 nsNavHistoryResult::GetRoot(nsINavHistoryContainerResultNode** aRoot)
 {
   if (!mRootNode) {
     NS_NOTREACHED("Root is null");
     *aRoot = nullptr;
     return NS_ERROR_FAILURE;
   }
-  return mRootNode->QueryInterface(NS_GET_IID(nsINavHistoryContainerResultNode),
-                                   reinterpret_cast<void**>(aRoot));
+  nsRefPtr<nsNavHistoryContainerResultNode> node(mRootNode);
+  node.forget(aRoot);
+  return NS_OK;
 }
 
 
 void
 nsNavHistoryResult::requestRefresh(nsNavHistoryContainerResultNode* aContainer)
 {
   // Don't add twice the same container.
   if (mRefreshParticipants.IndexOf(aContainer) == mRefreshParticipants.NoIndex)
--- a/widget/nsPrintSettingsImpl.cpp
+++ b/widget/nsPrintSettingsImpl.cpp
@@ -1105,18 +1105,19 @@ nsPrintSettings::GetPageRanges(nsTArray<
 {
   aPages.Clear();
   return NS_OK;
 }
 
 nsresult 
 nsPrintSettings::_Clone(nsIPrintSettings **_retval)
 {
-  nsPrintSettings* printSettings = new nsPrintSettings(*this);
-  return printSettings->QueryInterface(NS_GET_IID(nsIPrintSettings), (void**)_retval); // ref counts
+  nsRefPtr<nsPrintSettings> printSettings = new nsPrintSettings(*this);
+  printSettings.forget(_retval);
+  return NS_OK;
 }
 
 /* nsIPrintSettings clone (); */
 NS_IMETHODIMP 
 nsPrintSettings::Clone(nsIPrintSettings **_retval)
 {
   NS_ENSURE_ARG_POINTER(_retval);
   return _Clone(_retval);
--- a/widget/windows/nsImageClipboard.cpp
+++ b/widget/windows/nsImageClipboard.cpp
@@ -1,9 +1,9 @@
-/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  
 #include "nsImageClipboard.h"
 
 #include "gfxUtils.h"
 #include "mozilla/gfx/2D.h"
@@ -246,18 +246,19 @@ nsImageFromClipboard ::GetEncodedImageSt
       if (strcmp(aMIMEFormat, kJPGImageMime) == 0)
         encoderCID.AppendLiteral("image/jpeg");
       else
         encoderCID.Append(aMIMEFormat);
       nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get(), &rv);
       if (NS_SUCCEEDED(rv)){
         rv = encoder->InitFromData(rgbData, 0, width, height, 3 * width /* RGB * # pixels in a row */, 
                                    imgIEncoder::INPUT_FORMAT_RGB, EmptyString());
-        if (NS_SUCCEEDED(rv))
-          encoder->QueryInterface(NS_GET_IID(nsIInputStream), (void **) aInputStream);
+        if (NS_SUCCEEDED(rv)) {
+          encoder.forget(aInputStream);
+        }
       }
     }
     delete [] rgbData;
   } 
   else 
     rv = NS_ERROR_OUT_OF_MEMORY;
 
   return rv;
--- a/widget/windows/nsPrintSettingsWin.cpp
+++ b/widget/windows/nsPrintSettingsWin.cpp
@@ -113,18 +113,19 @@ NS_IMETHODIMP nsPrintSettingsWin::SetDev
   }
   return NS_OK;
 }
 
 //-------------------------------------------
 nsresult 
 nsPrintSettingsWin::_Clone(nsIPrintSettings **_retval)
 {
-  nsPrintSettingsWin* printSettings = new nsPrintSettingsWin(*this);
-  return printSettings->QueryInterface(NS_GET_IID(nsIPrintSettings), (void**)_retval); // ref counts
+  nsRefPtr<nsPrintSettingsWin> printSettings = new nsPrintSettingsWin(*this);
+  printSettings.forget(_retval);
+  return NS_OK;
 }
 
 //-------------------------------------------
 nsPrintSettingsWin& nsPrintSettingsWin::operator=(const nsPrintSettingsWin& rhs)
 {
   if (this == &rhs) {
     return *this;
   }
--- a/xpcom/io/nsAppFileLocationProvider.cpp
+++ b/xpcom/io/nsAppFileLocationProvider.cpp
@@ -223,17 +223,18 @@ nsAppFileLocationProvider::GetFile(const
     }
   } else if (nsCRT::strcmp(aProp, NS_APP_INSTALL_CLEANUP_DIR) == 0) {
     // This is cloned so that embeddors will have a hook to override
     // with their own cleanup dir.  See bugzilla bug #105087
     rv = CloneMozBinDirectory(getter_AddRefs(localFile));
   }
 
   if (localFile && NS_SUCCEEDED(rv)) {
-    return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)aResult);
+    localFile.forget(aResult);
+    return NS_OK;
   }
 
   return rv;
 }
 
 
 NS_METHOD
 nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile** aLocalFile)
--- a/xpfe/appshell/nsWindowMediator.cpp
+++ b/xpfe/appshell/nsWindowMediator.cpp
@@ -190,35 +190,31 @@ nsWindowMediator::GetInfoFor(nsIWidget *
 
 NS_IMETHODIMP
 nsWindowMediator::GetEnumerator(const char16_t* inType, nsISimpleEnumerator** outEnumerator)
 {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
   NS_ENSURE_ARG_POINTER(outEnumerator);
   NS_ENSURE_STATE(mReady);
 
-  nsAppShellWindowEnumerator *enumerator = new nsASDOMWindowEarlyToLateEnumerator(inType, *this);
-  if (enumerator)
-    return enumerator->QueryInterface(NS_GET_IID(nsISimpleEnumerator) , (void**)outEnumerator);
-
-  return NS_ERROR_OUT_OF_MEMORY;
+  nsRefPtr<nsAppShellWindowEnumerator> enumerator = new nsASDOMWindowEarlyToLateEnumerator(inType, *this);
+  enumerator.forget(outEnumerator);
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 nsWindowMediator::GetXULWindowEnumerator(const char16_t* inType, nsISimpleEnumerator** outEnumerator)
 {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
   NS_ENSURE_ARG_POINTER(outEnumerator);
   NS_ENSURE_STATE(mReady);
 
-  nsAppShellWindowEnumerator *enumerator = new nsASXULWindowEarlyToLateEnumerator(inType, *this);
-  if (enumerator)
-    return enumerator->QueryInterface(NS_GET_IID(nsISimpleEnumerator) , (void**)outEnumerator);
-
-  return NS_ERROR_OUT_OF_MEMORY;
+  nsRefPtr<nsAppShellWindowEnumerator> enumerator = new nsASXULWindowEarlyToLateEnumerator(inType, *this);
+  enumerator.forget(outEnumerator);
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 nsWindowMediator::GetZOrderDOMWindowEnumerator(
             const char16_t *aWindowType, bool aFrontToBack,
             nsISimpleEnumerator **_retval)
 {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());