Commit ee4707cb authored by David Van Cleve's avatar David Van Cleve Committed by Commit Bot

base/strings: Add more WARN_UNUSED_RESULTs

This adds WARN_UNUSED_RESULT to some methods in //base/strings
whose primary purpose is to return something useful (e.g. StrCat).
When the results of these methods go unused, it's usually caller error
and will be found by the caller later (for instance, when their tests
fail). WARN_UNUSED_RESULT makes this fail earlier, at compile time.

Precedent: Abseil's StrCat, etc., all use ABSL_MUST_USE_RESULT.
Change-Id: I8262b03bd844118c7337861f323417c2bb92ddda
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1925251Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: David Van Cleve <davidvc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#717633}
parent 41fbcd6e
......@@ -59,10 +59,13 @@ namespace base {
// for this call and generate slightly less code. This is something we can
// explore more in the future.
BASE_EXPORT std::string StrCat(span<const StringPiece> pieces);
BASE_EXPORT string16 StrCat(span<const StringPiece16> pieces);
BASE_EXPORT std::string StrCat(span<const std::string> pieces);
BASE_EXPORT string16 StrCat(span<const string16> pieces);
BASE_EXPORT std::string StrCat(span<const StringPiece> pieces)
WARN_UNUSED_RESULT;
BASE_EXPORT string16 StrCat(span<const StringPiece16> pieces)
WARN_UNUSED_RESULT;
BASE_EXPORT std::string StrCat(span<const std::string> pieces)
WARN_UNUSED_RESULT;
BASE_EXPORT string16 StrCat(span<const string16> pieces) WARN_UNUSED_RESULT;
// Initializer list forwards to the array version.
inline std::string StrCat(std::initializer_list<StringPiece> pieces) {
......
......@@ -43,17 +43,18 @@ enum SplitResult {
// To split on either commas or semicolons, keeping all whitespace:
//
// std::vector<std::string> tokens = base::SplitString(
// input, ",;", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
BASE_EXPORT std::vector<std::string> SplitString(
StringPiece input,
StringPiece separators,
WhitespaceHandling whitespace,
SplitResult result_type);
BASE_EXPORT std::vector<string16> SplitString(
StringPiece16 input,
StringPiece16 separators,
WhitespaceHandling whitespace,
SplitResult result_type);
// input, ", WARN_UNUSED_RESULT;", base::KEEP_WHITESPACE,
// base::SPLIT_WANT_ALL) WARN_UNUSED_RESULT;
BASE_EXPORT std::vector<std::string> SplitString(StringPiece input,
StringPiece separators,
WhitespaceHandling whitespace,
SplitResult result_type)
WARN_UNUSED_RESULT;
BASE_EXPORT std::vector<string16> SplitString(StringPiece16 input,
StringPiece16 separators,
WhitespaceHandling whitespace,
SplitResult result_type)
WARN_UNUSED_RESULT;
// Like SplitString above except it returns a vector of StringPieces which
// reference the original buffer without copying. Although you have to be
......@@ -71,12 +72,12 @@ BASE_EXPORT std::vector<StringPiece> SplitStringPiece(
StringPiece input,
StringPiece separators,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type) WARN_UNUSED_RESULT;
BASE_EXPORT std::vector<StringPiece16> SplitStringPiece(
StringPiece16 input,
StringPiece16 separators,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type) WARN_UNUSED_RESULT;
using StringPairs = std::vector<std::pair<std::string, std::string>>;
......@@ -103,12 +104,12 @@ BASE_EXPORT std::vector<string16> SplitStringUsingSubstr(
StringPiece16 input,
StringPiece16 delimiter,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type) WARN_UNUSED_RESULT;
BASE_EXPORT std::vector<std::string> SplitStringUsingSubstr(
StringPiece input,
StringPiece delimiter,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type) WARN_UNUSED_RESULT;
// Like SplitStringUsingSubstr above except it returns a vector of StringPieces
// which reference the original buffer without copying. Although you have to be
......@@ -126,36 +127,37 @@ BASE_EXPORT std::vector<StringPiece16> SplitStringPieceUsingSubstr(
StringPiece16 input,
StringPiece16 delimiter,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type) WARN_UNUSED_RESULT;
BASE_EXPORT std::vector<StringPiece> SplitStringPieceUsingSubstr(
StringPiece input,
StringPiece delimiter,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type) WARN_UNUSED_RESULT;
#if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING)
BASE_EXPORT std::vector<std::wstring> SplitString(WStringPiece input,
WStringPiece separators,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type)
WARN_UNUSED_RESULT;
BASE_EXPORT std::vector<WStringPiece> SplitStringPiece(
WStringPiece input,
WStringPiece separators,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type) WARN_UNUSED_RESULT;
BASE_EXPORT std::vector<std::wstring> SplitStringUsingSubstr(
WStringPiece input,
WStringPiece delimiter,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type) WARN_UNUSED_RESULT;
BASE_EXPORT std::vector<WStringPiece> SplitStringPieceUsingSubstr(
WStringPiece input,
WStringPiece delimiter,
WhitespaceHandling whitespace,
SplitResult result_type);
SplitResult result_type) WARN_UNUSED_RESULT;
#endif
} // namespace base
......
......@@ -31,14 +31,17 @@ namespace base {
// Converts between wide and UTF-8 representations of a string. On error, the
// result is system-dependent.
BASE_EXPORT std::string SysWideToUTF8(const std::wstring& wide);
BASE_EXPORT std::wstring SysUTF8ToWide(StringPiece utf8);
BASE_EXPORT std::string SysWideToUTF8(const std::wstring& wide)
WARN_UNUSED_RESULT;
BASE_EXPORT std::wstring SysUTF8ToWide(StringPiece utf8) WARN_UNUSED_RESULT;
// Converts between wide and the system multi-byte representations of a string.
// DANGER: This will lose information and can change (on Windows, this can
// change between reboots).
BASE_EXPORT std::string SysWideToNativeMB(const std::wstring& wide);
BASE_EXPORT std::wstring SysNativeMBToWide(StringPiece native_mb);
BASE_EXPORT std::string SysWideToNativeMB(const std::wstring& wide)
WARN_UNUSED_RESULT;
BASE_EXPORT std::wstring SysNativeMBToWide(StringPiece native_mb)
WARN_UNUSED_RESULT;
// Windows-specific ------------------------------------------------------------
......@@ -47,9 +50,11 @@ BASE_EXPORT std::wstring SysNativeMBToWide(StringPiece native_mb);
// Converts between 8-bit and wide strings, using the given code page. The
// code page identifier is one accepted by the Windows function
// MultiByteToWideChar().
BASE_EXPORT std::wstring SysMultiByteToWide(StringPiece mb, uint32_t code_page);
BASE_EXPORT std::wstring SysMultiByteToWide(StringPiece mb, uint32_t code_page)
WARN_UNUSED_RESULT;
BASE_EXPORT std::string SysWideToMultiByte(const std::wstring& wide,
uint32_t code_page);
uint32_t code_page)
WARN_UNUSED_RESULT;
#endif // defined(OS_WIN)
......@@ -61,21 +66,25 @@ BASE_EXPORT std::string SysWideToMultiByte(const std::wstring& wide,
// Creates a string, and returns it with a refcount of 1. You are responsible
// for releasing it. Returns NULL on failure.
BASE_EXPORT CFStringRef SysUTF8ToCFStringRef(StringPiece utf8);
BASE_EXPORT CFStringRef SysUTF16ToCFStringRef(StringPiece16 utf16);
BASE_EXPORT CFStringRef SysUTF8ToCFStringRef(StringPiece utf8)
WARN_UNUSED_RESULT;
BASE_EXPORT CFStringRef SysUTF16ToCFStringRef(StringPiece16 utf16)
WARN_UNUSED_RESULT;
// Same, but returns an autoreleased NSString.
BASE_EXPORT NSString* SysUTF8ToNSString(StringPiece utf8);
BASE_EXPORT NSString* SysUTF16ToNSString(StringPiece16 utf16);
BASE_EXPORT NSString* SysUTF8ToNSString(StringPiece utf8) WARN_UNUSED_RESULT;
BASE_EXPORT NSString* SysUTF16ToNSString(StringPiece16 utf16)
WARN_UNUSED_RESULT;
// Converts a CFStringRef to an STL string. Returns an empty string on failure.
BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref);
BASE_EXPORT string16 SysCFStringRefToUTF16(CFStringRef ref);
BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref)
WARN_UNUSED_RESULT;
BASE_EXPORT string16 SysCFStringRefToUTF16(CFStringRef ref) WARN_UNUSED_RESULT;
// Same, but accepts NSString input. Converts nil NSString* to the appropriate
// string type of length 0.
BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref);
BASE_EXPORT string16 SysNSStringToUTF16(NSString* ref);
BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref) WARN_UNUSED_RESULT;
BASE_EXPORT string16 SysNSStringToUTF16(NSString* ref) WARN_UNUSED_RESULT;
#endif // defined(OS_MACOSX)
......
......@@ -98,7 +98,7 @@ BASE_EXPORT bool UTF8ToUTF16WithAdjustments(
base::OffsetAdjuster::Adjustments* adjustments);
BASE_EXPORT string16 UTF8ToUTF16WithAdjustments(
const base::StringPiece& utf8,
base::OffsetAdjuster::Adjustments* adjustments);
base::OffsetAdjuster::Adjustments* adjustments) WARN_UNUSED_RESULT;
// As above, but instead internally examines the adjustments and applies them
// to |offsets_for_adjustment|. Input offsets greater than the length of the
// input string will be set to string16::npos. See comments by AdjustOffsets().
......
......@@ -23,31 +23,31 @@ namespace base {
// possible.
BASE_EXPORT bool WideToUTF8(const wchar_t* src, size_t src_len,
std::string* output);
BASE_EXPORT std::string WideToUTF8(WStringPiece wide);
BASE_EXPORT std::string WideToUTF8(WStringPiece wide) WARN_UNUSED_RESULT;
BASE_EXPORT bool UTF8ToWide(const char* src, size_t src_len,
std::wstring* output);
BASE_EXPORT std::wstring UTF8ToWide(StringPiece utf8);
BASE_EXPORT std::wstring UTF8ToWide(StringPiece utf8) WARN_UNUSED_RESULT;
BASE_EXPORT bool WideToUTF16(const wchar_t* src, size_t src_len,
string16* output);
BASE_EXPORT string16 WideToUTF16(WStringPiece wide);
BASE_EXPORT string16 WideToUTF16(WStringPiece wide) WARN_UNUSED_RESULT;
BASE_EXPORT bool UTF16ToWide(const char16* src, size_t src_len,
std::wstring* output);
BASE_EXPORT std::wstring UTF16ToWide(StringPiece16 utf16);
BASE_EXPORT std::wstring UTF16ToWide(StringPiece16 utf16) WARN_UNUSED_RESULT;
BASE_EXPORT bool UTF8ToUTF16(const char* src, size_t src_len, string16* output);
BASE_EXPORT string16 UTF8ToUTF16(StringPiece utf8);
BASE_EXPORT string16 UTF8ToUTF16(StringPiece utf8) WARN_UNUSED_RESULT;
BASE_EXPORT bool UTF16ToUTF8(const char16* src, size_t src_len,
std::string* output);
BASE_EXPORT std::string UTF16ToUTF8(StringPiece16 utf16);
BASE_EXPORT std::string UTF16ToUTF8(StringPiece16 utf16) WARN_UNUSED_RESULT;
// This converts an ASCII string, typically a hardcoded constant, to a UTF16
// string.
BASE_EXPORT string16 ASCIIToUTF16(StringPiece ascii);
BASE_EXPORT string16 ASCIIToUTF16(StringPiece ascii) WARN_UNUSED_RESULT;
// Converts to 7-bit ASCII by truncating. The result must be known to be ASCII
// beforehand.
BASE_EXPORT std::string UTF16ToASCII(StringPiece16 utf16);
BASE_EXPORT std::string UTF16ToASCII(StringPiece16 utf16) WARN_UNUSED_RESULT;
} // namespace base
......
......@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/macros.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
......@@ -14,10 +15,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
base::StringPiece string_piece_input(reinterpret_cast<const char*>(data),
size);
base::UTF8ToWide(string_piece_input);
ignore_result(base::UTF8ToWide(string_piece_input));
base::UTF8ToWide(reinterpret_cast<const char*>(data), size,
&output_std_wstring);
base::UTF8ToUTF16(string_piece_input);
ignore_result(base::UTF8ToUTF16(string_piece_input));
base::UTF8ToUTF16(reinterpret_cast<const char*>(data), size,
&output_string16);
......@@ -25,10 +26,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size % 2 == 0) {
base::StringPiece16 string_piece_input16(
reinterpret_cast<const base::char16*>(data), size / 2);
base::UTF16ToWide(output_string16);
ignore_result(base::UTF16ToWide(output_string16));
base::UTF16ToWide(reinterpret_cast<const base::char16*>(data), size / 2,
&output_std_wstring);
base::UTF16ToUTF8(string_piece_input16);
ignore_result(base::UTF16ToUTF8(string_piece_input16));
base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(data), size / 2,
&output_std_string);
}
......@@ -36,10 +37,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
// Test for wchar_t.
size_t wchar_t_size = sizeof(wchar_t);
if (size % wchar_t_size == 0) {
base::WideToUTF8(output_std_wstring);
ignore_result(base::WideToUTF8(output_std_wstring));
base::WideToUTF8(reinterpret_cast<const wchar_t*>(data),
size / wchar_t_size, &output_std_string);
base::WideToUTF16(output_std_wstring);
ignore_result(base::WideToUTF16(output_std_wstring));
base::WideToUTF16(reinterpret_cast<const wchar_t*>(data),
size / wchar_t_size, &output_string16);
}
......@@ -49,7 +50,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (base::IsStringASCII(string_piece_input)) {
output_string16 = base::ASCIIToUTF16(string_piece_input);
base::StringPiece16 string_piece_input16(output_string16);
base::UTF16ToASCII(string_piece_input16);
ignore_result(base::UTF16ToASCII(string_piece_input16));
}
return 0;
......
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