Skip to content

Commit 7ab570e

Browse files
author
Justin Lebar
committedJul 19, 2016
[ADT] Warn on unused results from ArrayRef and StringRef functions that read like they might mutate.
Summary: Functions like "slice" and "drop_front" sound like they might mutate the underlying object, but they don't. Warning on unused results would have saved me an hour yesterday, and I'm sure I'm not the only one. LLVM and Clang are clean wrt this warning after D22540. Reviewers: majnemer Subscribers: sanjoy, chandlerc, llvm-commits Differential Revision: https://reviews.llvm.org/D22541 llvm-svn: 276058
1 parent ea9598b commit 7ab570e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed
 

‎llvm/include/llvm/ADT/ArrayRef.h

+8
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,29 @@ namespace llvm {
161161
}
162162

163163
/// slice(n) - Chop off the first N elements of the array.
164+
LLVM_ATTRIBUTE_UNUSED_RESULT
164165
ArrayRef<T> slice(size_t N) const {
165166
assert(N <= size() && "Invalid specifier");
166167
return ArrayRef<T>(data()+N, size()-N);
167168
}
168169

169170
/// slice(n, m) - Chop off the first N elements of the array, and keep M
170171
/// elements in the array.
172+
LLVM_ATTRIBUTE_UNUSED_RESULT
171173
ArrayRef<T> slice(size_t N, size_t M) const {
172174
assert(N+M <= size() && "Invalid specifier");
173175
return ArrayRef<T>(data()+N, M);
174176
}
175177

176178
/// \brief Drop the first \p N elements of the array.
179+
LLVM_ATTRIBUTE_UNUSED_RESULT
177180
ArrayRef<T> drop_front(size_t N = 1) const {
178181
assert(size() >= N && "Dropping more elements than exist");
179182
return slice(N, size() - N);
180183
}
181184

182185
/// \brief Drop the last \p N elements of the array.
186+
LLVM_ATTRIBUTE_UNUSED_RESULT
183187
ArrayRef<T> drop_back(size_t N = 1) const {
184188
assert(size() >= N && "Dropping more elements than exist");
185189
return slice(0, size() - N);
@@ -279,24 +283,28 @@ namespace llvm {
279283
}
280284

281285
/// slice(n) - Chop off the first N elements of the array.
286+
LLVM_ATTRIBUTE_UNUSED_RESULT
282287
MutableArrayRef<T> slice(size_t N) const {
283288
assert(N <= this->size() && "Invalid specifier");
284289
return MutableArrayRef<T>(data()+N, this->size()-N);
285290
}
286291

287292
/// slice(n, m) - Chop off the first N elements of the array, and keep M
288293
/// elements in the array.
294+
LLVM_ATTRIBUTE_UNUSED_RESULT
289295
MutableArrayRef<T> slice(size_t N, size_t M) const {
290296
assert(N+M <= this->size() && "Invalid specifier");
291297
return MutableArrayRef<T>(data()+N, M);
292298
}
293299

294300
/// \brief Drop the first \p N elements of the array.
301+
LLVM_ATTRIBUTE_UNUSED_RESULT
295302
MutableArrayRef<T> drop_front(size_t N = 1) const {
296303
assert(this->size() >= N && "Dropping more elements than exist");
297304
return slice(N, this->size() - N);
298305
}
299306

307+
LLVM_ATTRIBUTE_UNUSED_RESULT
300308
MutableArrayRef<T> drop_back(size_t N = 1) const {
301309
assert(this->size() >= N && "Dropping more elements than exist");
302310
return slice(0, this->size() - N);

‎llvm/include/llvm/ADT/StringRef.h

+10
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ namespace llvm {
422422
/// exceeds the number of characters remaining in the string, the string
423423
/// suffix (starting with \p Start) will be returned.
424424
LLVM_ATTRIBUTE_ALWAYS_INLINE
425+
LLVM_ATTRIBUTE_UNUSED_RESULT
425426
StringRef substr(size_t Start, size_t N = npos) const {
426427
Start = std::min(Start, Length);
427428
return StringRef(Data + Start, std::min(N, Length - Start));
@@ -430,6 +431,7 @@ namespace llvm {
430431
/// Return a StringRef equal to 'this' but with the first \p N elements
431432
/// dropped.
432433
LLVM_ATTRIBUTE_ALWAYS_INLINE
434+
LLVM_ATTRIBUTE_UNUSED_RESULT
433435
StringRef drop_front(size_t N = 1) const {
434436
assert(size() >= N && "Dropping more elements than exist");
435437
return substr(N);
@@ -438,6 +440,7 @@ namespace llvm {
438440
/// Return a StringRef equal to 'this' but with the last \p N elements
439441
/// dropped.
440442
LLVM_ATTRIBUTE_ALWAYS_INLINE
443+
LLVM_ATTRIBUTE_UNUSED_RESULT
441444
StringRef drop_back(size_t N = 1) const {
442445
assert(size() >= N && "Dropping more elements than exist");
443446
return substr(0, size()-N);
@@ -455,6 +458,7 @@ namespace llvm {
455458
/// will be returned. If this is less than \p Start, an empty string will
456459
/// be returned.
457460
LLVM_ATTRIBUTE_ALWAYS_INLINE
461+
LLVM_ATTRIBUTE_UNUSED_RESULT
458462
StringRef slice(size_t Start, size_t End) const {
459463
Start = std::min(Start, Length);
460464
End = std::min(std::max(Start, End), Length);
@@ -549,36 +553,42 @@ namespace llvm {
549553

550554
/// Return string with consecutive \p Char characters starting from the
551555
/// the left removed.
556+
LLVM_ATTRIBUTE_UNUSED_RESULT
552557
StringRef ltrim(char Char) const {
553558
return drop_front(std::min(Length, find_first_not_of(Char)));
554559
}
555560

556561
/// Return string with consecutive characters in \p Chars starting from
557562
/// the left removed.
563+
LLVM_ATTRIBUTE_UNUSED_RESULT
558564
StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
559565
return drop_front(std::min(Length, find_first_not_of(Chars)));
560566
}
561567

562568
/// Return string with consecutive \p Char characters starting from the
563569
/// right removed.
570+
LLVM_ATTRIBUTE_UNUSED_RESULT
564571
StringRef rtrim(char Char) const {
565572
return drop_back(Length - std::min(Length, find_last_not_of(Char) + 1));
566573
}
567574

568575
/// Return string with consecutive characters in \p Chars starting from
569576
/// the right removed.
577+
LLVM_ATTRIBUTE_UNUSED_RESULT
570578
StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
571579
return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
572580
}
573581

574582
/// Return string with consecutive \p Char characters starting from the
575583
/// left and right removed.
584+
LLVM_ATTRIBUTE_UNUSED_RESULT
576585
StringRef trim(char Char) const {
577586
return ltrim(Char).rtrim(Char);
578587
}
579588

580589
/// Return string with consecutive characters in \p Chars starting from
581590
/// the left and right removed.
591+
LLVM_ATTRIBUTE_UNUSED_RESULT
582592
StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
583593
return ltrim(Chars).rtrim(Chars);
584594
}

0 commit comments

Comments
 (0)
Please sign in to comment.