Commit 36a2733b authored by ricea@chromium.org's avatar ricea@chromium.org

Require the result of StringPrintf to be used.

When refactoring code it is easy to accidentally disregard the output of
base::StringPrintf. The code will still look superficially correct, and may even
pass code review (as happened with https://codereview.chromium.org/436133002/ ).

This type of bug can be prevented by annotating StringPrintf with
WARN_UNUSED_RESULT. Despite the name, this causes a compile error in our build
configuration (except on Windows, where it is ignored). This should ensure that
such errors are caught by the CQ if not sooner.

TEST=build all; base_unittests
BUG=400663

Review URL: https://codereview.chromium.org/440013003

Cr-Commit-Position: refs/heads/master@{#288353}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288353 0039d316-1c4b-4281-b951-d872f2087c98
parent aa45d8ae
...@@ -16,16 +16,16 @@ namespace base { ...@@ -16,16 +16,16 @@ namespace base {
// Return a C++ string given printf-like input. // Return a C++ string given printf-like input.
BASE_EXPORT std::string StringPrintf(const char* format, ...) BASE_EXPORT std::string StringPrintf(const char* format, ...)
PRINTF_FORMAT(1, 2); PRINTF_FORMAT(1, 2) WARN_UNUSED_RESULT;
// OS_ANDROID's libc does not support wchar_t, so several overloads are omitted. // OS_ANDROID's libc does not support wchar_t, so several overloads are omitted.
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
BASE_EXPORT std::wstring StringPrintf(const wchar_t* format, ...) BASE_EXPORT std::wstring StringPrintf(const wchar_t* format, ...)
WPRINTF_FORMAT(1, 2); WPRINTF_FORMAT(1, 2) WARN_UNUSED_RESULT;
#endif #endif
// Return a C++ string given vprintf-like input. // Return a C++ string given vprintf-like input.
BASE_EXPORT std::string StringPrintV(const char* format, va_list ap) BASE_EXPORT std::string StringPrintV(const char* format, va_list ap)
PRINTF_FORMAT(1, 0); PRINTF_FORMAT(1, 0) WARN_UNUSED_RESULT;
// Store result into a supplied string and return it. // Store result into a supplied string and return it.
BASE_EXPORT const std::string& SStringPrintf(std::string* dst, BASE_EXPORT const std::string& SStringPrintf(std::string* dst,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment