Skip to content

Commit c67e2d9

Browse files
[ADT] Add [[nodiscard]] to SmallString (NFC)
1 parent 9d6837d commit c67e2d9

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

llvm/include/llvm/ADT/SmallString.h

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,26 @@ class SmallString : public SmallVector<char, InternalLen> {
8989

9090
/// Check for string equality. This is more efficient than compare() when
9191
/// the relative ordering of inequal strings isn't needed.
92-
bool equals(StringRef RHS) const {
93-
return str().equals(RHS);
94-
}
92+
[[nodiscard]] bool equals(StringRef RHS) const { return str().equals(RHS); }
9593

9694
/// Check for string equality, ignoring case.
97-
bool equals_insensitive(StringRef RHS) const {
95+
[[nodiscard]] bool equals_insensitive(StringRef RHS) const {
9896
return str().equals_insensitive(RHS);
9997
}
10098

10199
/// compare - Compare two strings; the result is negative, zero, or positive
102100
/// if this string is lexicographically less than, equal to, or greater than
103101
/// the \p RHS.
104-
int compare(StringRef RHS) const {
105-
return str().compare(RHS);
106-
}
102+
[[nodiscard]] int compare(StringRef RHS) const { return str().compare(RHS); }
107103

108104
/// compare_insensitive - Compare two strings, ignoring case.
109-
int compare_insensitive(StringRef RHS) const {
105+
[[nodiscard]] int compare_insensitive(StringRef RHS) const {
110106
return str().compare_insensitive(RHS);
111107
}
112108

113109
/// compare_numeric - Compare two strings, treating sequences of digits as
114110
/// numbers.
115-
int compare_numeric(StringRef RHS) const {
111+
[[nodiscard]] int compare_numeric(StringRef RHS) const {
116112
return str().compare_numeric(RHS);
117113
}
118114

@@ -121,10 +117,14 @@ class SmallString : public SmallVector<char, InternalLen> {
121117
/// @{
122118

123119
/// starts_with - Check if this string starts with the given \p Prefix.
124-
bool starts_with(StringRef Prefix) const { return str().starts_with(Prefix); }
120+
[[nodiscard]] bool starts_with(StringRef Prefix) const {
121+
return str().starts_with(Prefix);
122+
}
125123

126124
/// ends_with - Check if this string ends with the given \p Suffix.
127-
bool ends_with(StringRef Suffix) const { return str().ends_with(Suffix); }
125+
[[nodiscard]] bool ends_with(StringRef Suffix) const {
126+
return str().ends_with(Suffix);
127+
}
128128

129129
/// @}
130130
/// @name String Searching
@@ -134,74 +134,74 @@ class SmallString : public SmallVector<char, InternalLen> {
134134
///
135135
/// \return - The index of the first occurrence of \p C, or npos if not
136136
/// found.
137-
size_t find(char C, size_t From = 0) const {
137+
[[nodiscard]] size_t find(char C, size_t From = 0) const {
138138
return str().find(C, From);
139139
}
140140

141141
/// Search for the first string \p Str in the string.
142142
///
143143
/// \returns The index of the first occurrence of \p Str, or npos if not
144144
/// found.
145-
size_t find(StringRef Str, size_t From = 0) const {
145+
[[nodiscard]] size_t find(StringRef Str, size_t From = 0) const {
146146
return str().find(Str, From);
147147
}
148148

149149
/// Search for the last character \p C in the string.
150150
///
151151
/// \returns The index of the last occurrence of \p C, or npos if not
152152
/// found.
153-
size_t rfind(char C, size_t From = StringRef::npos) const {
153+
[[nodiscard]] size_t rfind(char C, size_t From = StringRef::npos) const {
154154
return str().rfind(C, From);
155155
}
156156

157157
/// Search for the last string \p Str in the string.
158158
///
159159
/// \returns The index of the last occurrence of \p Str, or npos if not
160160
/// found.
161-
size_t rfind(StringRef Str) const {
162-
return str().rfind(Str);
163-
}
161+
[[nodiscard]] size_t rfind(StringRef Str) const { return str().rfind(Str); }
164162

165163
/// Find the first character in the string that is \p C, or npos if not
166164
/// found. Same as find.
167-
size_t find_first_of(char C, size_t From = 0) const {
165+
[[nodiscard]] size_t find_first_of(char C, size_t From = 0) const {
168166
return str().find_first_of(C, From);
169167
}
170168

171169
/// Find the first character in the string that is in \p Chars, or npos if
172170
/// not found.
173171
///
174172
/// Complexity: O(size() + Chars.size())
175-
size_t find_first_of(StringRef Chars, size_t From = 0) const {
173+
[[nodiscard]] size_t find_first_of(StringRef Chars, size_t From = 0) const {
176174
return str().find_first_of(Chars, From);
177175
}
178176

179177
/// Find the first character in the string that is not \p C or npos if not
180178
/// found.
181-
size_t find_first_not_of(char C, size_t From = 0) const {
179+
[[nodiscard]] size_t find_first_not_of(char C, size_t From = 0) const {
182180
return str().find_first_not_of(C, From);
183181
}
184182

185183
/// Find the first character in the string that is not in the string
186184
/// \p Chars, or npos if not found.
187185
///
188186
/// Complexity: O(size() + Chars.size())
189-
size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
187+
[[nodiscard]] size_t find_first_not_of(StringRef Chars,
188+
size_t From = 0) const {
190189
return str().find_first_not_of(Chars, From);
191190
}
192191

193192
/// Find the last character in the string that is \p C, or npos if not
194193
/// found.
195-
size_t find_last_of(char C, size_t From = StringRef::npos) const {
194+
[[nodiscard]] size_t find_last_of(char C,
195+
size_t From = StringRef::npos) const {
196196
return str().find_last_of(C, From);
197197
}
198198

199199
/// Find the last character in the string that is in \p C, or npos if not
200200
/// found.
201201
///
202202
/// Complexity: O(size() + Chars.size())
203-
size_t find_last_of(
204-
StringRef Chars, size_t From = StringRef::npos) const {
203+
[[nodiscard]] size_t find_last_of(StringRef Chars,
204+
size_t From = StringRef::npos) const {
205205
return str().find_last_of(Chars, From);
206206
}
207207

@@ -210,15 +210,11 @@ class SmallString : public SmallVector<char, InternalLen> {
210210
/// @{
211211

212212
/// Return the number of occurrences of \p C in the string.
213-
size_t count(char C) const {
214-
return str().count(C);
215-
}
213+
[[nodiscard]] size_t count(char C) const { return str().count(C); }
216214

217215
/// Return the number of non-overlapped occurrences of \p Str in the
218216
/// string.
219-
size_t count(StringRef Str) const {
220-
return str().count(Str);
221-
}
217+
[[nodiscard]] size_t count(StringRef Str) const { return str().count(Str); }
222218

223219
/// @}
224220
/// @name Substring Operations
@@ -233,7 +229,8 @@ class SmallString : public SmallVector<char, InternalLen> {
233229
/// \param N The number of characters to included in the substring. If \p N
234230
/// exceeds the number of characters remaining in the string, the string
235231
/// suffix (starting with \p Start) will be returned.
236-
StringRef substr(size_t Start, size_t N = StringRef::npos) const {
232+
[[nodiscard]] StringRef substr(size_t Start,
233+
size_t N = StringRef::npos) const {
237234
return str().substr(Start, N);
238235
}
239236

@@ -247,14 +244,16 @@ class SmallString : public SmallVector<char, InternalLen> {
247244
/// substring. If this is npos, or less than \p Start, or exceeds the
248245
/// number of characters remaining in the string, the string suffix
249246
/// (starting with \p Start) will be returned.
250-
StringRef slice(size_t Start, size_t End) const {
247+
[[nodiscard]] StringRef slice(size_t Start, size_t End) const {
251248
return str().slice(Start, End);
252249
}
253250

254251
// Extra methods.
255252

256253
/// Explicit conversion to StringRef.
257-
StringRef str() const { return StringRef(this->data(), this->size()); }
254+
[[nodiscard]] StringRef str() const {
255+
return StringRef(this->data(), this->size());
256+
}
258257

259258
// TODO: Make this const, if it's safe...
260259
const char* c_str() {

0 commit comments

Comments
 (0)