907 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-standalone-empty %t2 3namespace std {4template <typename T>5struct vector {6 bool empty();7};8 9template <typename T>10struct vector_with_clear {11 bool empty();12 void clear();13};14 15template <typename T>16struct vector_with_void_empty {17 void empty();18 void clear();19};20 21template <typename T>22struct vector_with_int_empty {23 int empty();24 void clear();25};26 27template <typename T>28struct vector_with_clear_args {29 bool empty();30 void clear(int i);31};32 33template <typename T>34struct vector_with_clear_variable {35 bool empty();36 int clear;37};38 39template <typename T>40bool empty(T &&);41 42} // namespace std43 44namespace absl {45struct string {46 bool empty();47};48 49struct string_with_clear {50 bool empty();51 void clear();52};53 54struct string_with_void_empty {55 void empty();56 void clear();57};58 59struct string_with_int_empty {60 int empty();61 void clear();62};63 64struct string_with_clear_args {65 bool empty();66 void clear(int i);67};68 69struct string_with_clear_variable {70 bool empty();71 int clear;72};73 74template <class T>75bool empty(T &&);76} // namespace absl77 78namespace test {79template <class T>80void empty(T &&);81} // namespace test82 83namespace test_no_args {84bool empty();85} // namespace test_no_args86 87namespace base {88template <typename T>89struct base_vector {90 void clear();91};92 93template <typename T>94struct base_vector_clear_with_args {95 void clear(int i);96};97 98template <typename T>99struct base_vector_clear_variable {100 int clear;101};102 103struct base_vector_non_dependent {104 void clear();105};106 107template <typename T>108struct vector : base_vector<T> {109 bool empty();110};111 112template <typename T>113struct vector_clear_with_args : base_vector_clear_with_args<T> {114 bool empty();115};116 117template <typename T>118struct vector_clear_variable : base_vector_clear_variable<T> {119 bool empty();120};121 122template <typename T>123struct vector_non_dependent : base_vector_non_dependent {124 bool empty();125};126 127template <typename T>128bool empty(T &&);129 130} // namespace base131 132namespace qualifiers {133template <typename T>134struct vector_with_const_clear {135 bool empty() const;136 void clear() const;137};138 139template <typename T>140struct vector_with_const_empty {141 bool empty() const;142 void clear();143};144 145template <typename T>146struct vector_with_volatile_clear {147 bool empty() volatile;148 void clear() volatile;149};150 151template <typename T>152struct vector_with_volatile_empty {153 bool empty() volatile;154 void clear();155};156 157template <typename T>158bool empty(T &&);159} // namespace qualifiers160 161 162bool test_member_empty() {163 {164 std::vector<int> v;165 v.empty();166 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]167 }168 169 {170 std::vector_with_void_empty<int> v;171 v.empty();172 // no-warning173 }174 175 {176 std::vector_with_clear<int> v;177 v.empty();178 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()'; did you mean 'clear()'? [bugprone-standalone-empty]179 // CHECK-FIXES: v.clear();180 }181 182 {183 std::vector_with_int_empty<int> v;184 v.empty();185 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()'; did you mean 'clear()'? [bugprone-standalone-empty]186 // CHECK-FIXES: v.clear();187 }188 189 {190 std::vector_with_clear_args<int> v;191 v.empty();192 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]193 }194 195 {196 std::vector_with_clear_variable<int> v;197 v.empty();198 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]199 }200 201 {202 absl::string s;203 s.empty();204 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]205 }206 207 {208 absl::string_with_void_empty s;209 s.empty();210 // no-warning211 }212 213 {214 absl::string_with_clear s;215 s.empty();216 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()'; did you mean 'clear()'? [bugprone-standalone-empty]217 // CHECK-FIXES: s.clear();218 }219 220 {221 absl::string_with_int_empty s;222 s.empty();223 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()'; did you mean 'clear()'? [bugprone-standalone-empty]224 // CHECK-FIXES: s.clear();225 }226 227 {228 absl::string_with_clear_args s;229 s.empty();230 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]231 }232 233 {234 absl::string_with_clear_variable s;235 s.empty();236 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]237 }238 239 {240 std::vector<int> v;241 return v.empty();242 // no-warning243 }244 245 {246 std::vector_with_clear<int> v;247 return v.empty();248 // no-warning249 }250 251 {252 std::vector_with_int_empty<int> v;253 return v.empty();254 // no-warning255 }256 257 {258 std::vector_with_clear_args<int> v;259 return v.empty();260 // no-warning261 }262 263 {264 std::vector_with_clear_variable<int> v;265 return v.empty();266 // no-warning267 }268 269 {270 absl::string s;271 return s.empty();272 // no-warning273 }274 275 {276 absl::string_with_clear s;277 return s.empty();278 // no-warning279 }280 281 {282 absl::string_with_int_empty s;283 return s.empty();284 // no-warning285 }286 287 {288 absl::string_with_clear_args s;289 return s.empty();290 // no-warning291 }292 293 {294 absl::string_with_clear_variable s;295 return s.empty();296 // no-warning297 }298}299 300bool test_qualified_empty() {301 {302 absl::string_with_clear v;303 std::empty(v);304 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty'; did you mean 'clear()'? [bugprone-standalone-empty]305 // CHECK-FIXES: v.clear();306 307 absl::empty(v);308 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty'; did you mean 'clear()'? [bugprone-standalone-empty]309 // CHECK-FIXES: v.clear();310 311 test::empty(v);312 // no-warning313 314 test_no_args::empty();315 // no-warning316 }317 318 {319 absl::string s;320 std::empty(s);321 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]322 }323 324 {325 std::empty(0);326 // no-warning327 absl::empty(nullptr);328 // no-warning329 }330 331 {332 absl::string_with_clear s;333 return std::empty(s);334 // no-warning335 return absl::empty(s);336 // no-warning337 }338 339 {340 absl::string s;341 return std::empty(s);342 // no-warning343 }344 345 {346 return std::empty(0);347 // no-warning348 return absl::empty(nullptr);349 // no-warning350 }351}352 353bool test_unqualified_empty() {354 {355 std::vector<int> v;356 empty(v);357 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]358 }359 360 {361 std::vector_with_void_empty<int> v;362 empty(v);363 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty'; did you mean 'clear()'? [bugprone-standalone-empty]364 // CHECK-FIXES: v.clear();365 }366 367 {368 std::vector_with_clear<int> v;369 empty(v);370 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty'; did you mean 'clear()'? [bugprone-standalone-empty]371 // CHECK-FIXES: v.clear();372 }373 374 {375 std::vector_with_int_empty<int> v;376 empty(v);377 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty'; did you mean 'clear()'? [bugprone-standalone-empty]378 // CHECK-FIXES: v.clear();379 }380 381 {382 std::vector_with_clear_args<int> v;383 empty(v);384 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]385 }386 387 {388 std::vector_with_clear_variable<int> v;389 empty(v);390 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]391 }392 393 {394 absl::string s;395 empty(s);396 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty' [bugprone-standalone-empty]397 }398 399 {400 absl::string_with_void_empty s;401 empty(s);402 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty'; did you mean 'clear()'? [bugprone-standalone-empty]403 // CHECK-FIXES: s.clear();404 }405 406 {407 absl::string_with_clear s;408 empty(s);409 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty'; did you mean 'clear()'? [bugprone-standalone-empty]410 // CHECK-FIXES: s.clear();411 }412 413 {414 absl::string_with_int_empty s;415 empty(s);416 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty'; did you mean 'clear()'? [bugprone-standalone-empty]417 // CHECK-FIXES: s.clear();418 }419 420 {421 absl::string_with_clear_args s;422 empty(s);423 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty' [bugprone-standalone-empty]424 }425 426 {427 absl::string_with_clear_variable s;428 empty(s);429 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty' [bugprone-standalone-empty]430 }431 432 {433 std::vector<int> v;434 using std::empty;435 empty(v);436 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]437 }438 439 {440 std::vector_with_clear<int> v;441 using std::empty;442 empty(v);443 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty'; did you mean 'clear()'? [bugprone-standalone-empty]444 // CHECK-FIXES: v.clear();445 }446 447 {448 absl::string s;449 using absl::empty;450 empty(s);451 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty' [bugprone-standalone-empty]452 }453 454 {455 absl::string_with_clear s;456 using absl::empty;457 empty(s);458 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty'; did you mean 'clear()'? [bugprone-standalone-empty]459 // CHECK-FIXES: s.clear();460 }461 462 {463 std::vector<int> v;464 return empty(v);465 // no-warning466 }467 468 {469 std::vector_with_void_empty<int> v;470 return empty(v);471 // no-warning472 }473 474 {475 std::vector_with_clear<int> v;476 return empty(v);477 // no-warning478 }479 480 {481 std::vector_with_int_empty<int> v;482 return empty(v);483 // no-warning484 }485 486 {487 std::vector_with_clear_args<int> v;488 return empty(v);489 // no-warning490 }491 492 {493 std::vector_with_clear_variable<int> v;494 return empty(v);495 // no-warning496 }497 498 {499 absl::string s;500 return empty(s);501 // no-warning502 }503 504 {505 absl::string_with_void_empty s;506 return empty(s);507 // no-warning508 }509 510 {511 absl::string_with_clear s;512 return empty(s);513 // no-warning514 }515 516 {517 absl::string_with_int_empty s;518 return empty(s);519 // no-warning520 }521 522 {523 absl::string_with_clear_args s;524 return empty(s);525 // no-warning526 }527 528 {529 absl::string_with_clear_variable s;530 return empty(s);531 // no-warning532 }533 534 {535 std::vector<int> v;536 using std::empty;537 return empty(v);538 // no-warning539 }540 541 {542 std::vector_with_clear<int> v;543 using std::empty;544 return empty(v);545 // no-warning546 }547 548 {549 absl::string s;550 using absl::empty;551 return empty(s);552 // no-warning553 }554 555 {556 absl::string_with_clear s;557 using absl::empty;558 return empty(s);559 // no-warning560 }561}562 563void test_empty_method_expressions() {564 std::vector<int> v;565 bool EmptyReturn(v.empty());566 // no-warning567 568 (void)v.empty();569 // no-warning570 571 // Don't warn in the if condition.572 if (v.empty()) v.empty();573 // CHECK-MESSAGES: :[[#@LINE-1]]:18: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]574 575 // Don't warn in the for condition.576 for(v.empty();v.empty();v.empty()) v.empty();577 // CHECK-MESSAGES: :[[#@LINE-1]]:7: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]578 // CHECK-MESSAGES: :[[#@LINE-2]]:27: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]579 // CHECK-MESSAGES: :[[#@LINE-3]]:38: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]580 581 // Don't warn in the while condition.582 while(v.empty()) v.empty();583 // CHECK-MESSAGES: :[[#@LINE-1]]:20: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]584 585 // Don't warn in the do-while condition.586 do v.empty(); while(v.empty());587 // CHECK-MESSAGES: :[[#@LINE-1]]:6: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]588 589 // Don't warn in the switch expression.590 switch(v.empty()) {591 // no-warning592 case true:593 v.empty();594 // CHECK-MESSAGES: :[[#@LINE-1]]:7: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]595 }596 597 // Don't warn in the return expression, which is the last statement.598 bool StmtExprReturn = ({v.empty(); v.empty();});599 // CHECK-MESSAGES: :[[#@LINE-1]]:27: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]600}601 602void test_empty_expressions() {603 absl::string s;604 bool test(std::empty(s));605 // no-warning606 607 (void)std::empty(s);608 // no-warning609 610 if (std::empty(s)) std::empty(s);611 // CHECK-MESSAGES: :[[#@LINE-1]]:22: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]612 613 for(std::empty(s);std::empty(s);std::empty(s)) std::empty(s);614 // CHECK-MESSAGES: :[[#@LINE-1]]:7: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]615 // CHECK-MESSAGES: :[[#@LINE-2]]:35: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]616 // CHECK-MESSAGES: :[[#@LINE-3]]:50: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]617 618 while(std::empty(s)) std::empty(s);619 // CHECK-MESSAGES: :[[#@LINE-1]]:24: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]620 621 do std::empty(s); while(std::empty(s));622 // CHECK-MESSAGES: :[[#@LINE-1]]:6: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]623 624 switch(std::empty(s)) {625 // no-warning626 case true:627 std::empty(s);628 // CHECK-MESSAGES: :[[#@LINE-1]]:7: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]629 }630 631 bool StmtExprReturn = ({std::empty(s); std::empty(s);});632 // CHECK-MESSAGES: :[[#@LINE-1]]:27: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]633}634 635bool test_clear_in_base_class() {636 {637 base::vector<int> v;638 v.empty();639 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()'; did you mean 'clear()'? [bugprone-standalone-empty]640 // CHECK-FIXES: v.clear();641 }642 643 {644 base::vector_non_dependent<int> v;645 v.empty();646 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()'; did you mean 'clear()'? [bugprone-standalone-empty]647 // CHECK-FIXES: v.clear();648 }649 650 {651 base::vector_clear_with_args<int> v;652 v.empty();653 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]654 }655 656 {657 base::vector_clear_variable<int> v;658 v.empty();659 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]660 }661 662 {663 base::vector<int> v;664 empty(v);665 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'base::empty'; did you mean 'clear()'? [bugprone-standalone-empty]666 // CHECK-FIXES: v.clear();667 }668 669 {670 base::vector_non_dependent<int> v;671 empty(v);672 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'base::empty'; did you mean 'clear()'? [bugprone-standalone-empty]673 // CHECK-FIXES: v.clear();674 }675 676 {677 base::vector_clear_with_args<int> v;678 empty(v);679 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'base::empty' [bugprone-standalone-empty]680 }681 682 {683 base::vector_clear_variable<int> v;684 empty(v);685 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'base::empty' [bugprone-standalone-empty]686 }687 688 {689 base::vector<int> v;690 return v.empty();691 // no-warning692 }693 694 {695 base::vector_non_dependent<int> v;696 return v.empty();697 // no-warning698 }699 700 {701 base::vector_clear_with_args<int> v;702 return v.empty();703 // no-warning704 }705 706 {707 base::vector_clear_variable<int> v;708 return v.empty();709 // no-warning710 }711 712 {713 base::vector<int> v;714 return empty(v);715 // no-warning716 }717 718 {719 base::vector_non_dependent<int> v;720 return empty(v);721 // no-warning722 }723 724 {725 base::vector_clear_with_args<int> v;726 return empty(v);727 // no-warning728 }729 730 {731 base::vector_clear_variable<int> v;732 return empty(v);733 // no-warning734 }735}736 737bool test_clear_with_qualifiers() {738 {739 qualifiers::vector_with_const_clear<int> v;740 v.empty();741 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]742 }743 744 {745 const qualifiers::vector_with_const_clear<int> v;746 v.empty();747 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]748 }749 750 {751 const qualifiers::vector_with_const_empty<int> v;752 v.empty();753 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]754 }755 756 {757 qualifiers::vector_with_const_clear<int> v;758 empty(v);759 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'qualifiers::empty' [bugprone-standalone-empty]760 }761 762 {763 const qualifiers::vector_with_const_clear<int> v;764 empty(v);765 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'qualifiers::empty' [bugprone-standalone-empty]766 }767 768 {769 const std::vector_with_clear<int> v;770 empty(v);771 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]772 }773 774 {775 qualifiers::vector_with_volatile_clear<int> v;776 v.empty();777 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()'; did you mean 'clear()'? [bugprone-standalone-empty]778 // CHECK-FIXES: v.clear();779 }780 781 {782 volatile qualifiers::vector_with_volatile_clear<int> v;783 v.empty();784 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()'; did you mean 'clear()'? [bugprone-standalone-empty]785 // CHECK-FIXES: v.clear();786 }787 788 {789 volatile qualifiers::vector_with_volatile_empty<int> v;790 v.empty();791 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]792 }793 794 {795 qualifiers::vector_with_volatile_clear<int> v;796 empty(v);797 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'qualifiers::empty'; did you mean 'clear()'? [bugprone-standalone-empty]798 // CHECK-FIXES: v.clear();799 }800 801 {802 volatile qualifiers::vector_with_volatile_clear<int> v;803 empty(v);804 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'qualifiers::empty'; did you mean 'clear()'? [bugprone-standalone-empty]805 // CHECK-FIXES: v.clear();806 }807 808 {809 volatile std::vector_with_clear<int> v;810 empty(v);811 // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty]812 }813 814 {815 qualifiers::vector_with_const_clear<int> v;816 return v.empty();817 // no-warning818 }819 820 {821 const qualifiers::vector_with_const_clear<int> v;822 return v.empty();823 // no-warning824 }825 826 {827 const qualifiers::vector_with_const_empty<int> v;828 return v.empty();829 // no-warning830 }831 832 {833 qualifiers::vector_with_const_clear<int> v;834 return empty(v);835 // no-warning836 }837 838 {839 const qualifiers::vector_with_const_clear<int> v;840 return empty(v);841 // no-warning842 }843 844 {845 const std::vector_with_clear<int> v;846 return empty(v);847 // no-warning848 }849 850 {851 qualifiers::vector_with_volatile_clear<int> v;852 return v.empty();853 // no-warning854 }855 856 {857 volatile qualifiers::vector_with_volatile_clear<int> v;858 return v.empty();859 // no-warning860 }861 862 {863 volatile qualifiers::vector_with_volatile_empty<int> v;864 return v.empty();865 // no-warning866 }867 868 {869 qualifiers::vector_with_volatile_clear<int> v;870 return empty(v);871 // no-warning872 }873 874 {875 volatile qualifiers::vector_with_volatile_clear<int> v;876 return empty(v);877 // no-warning878 }879 880 {881 volatile std::vector_with_clear<int> v;882 return empty(v);883 // no-warning884 }885}886 887namespace user_lib {888template <typename T>889struct vector {890 bool empty();891 bool test_empty_inside_impl() {892 empty();893 // no-warning894 return empty();895 // no-warning896 }897};898} // namespace user_lib899 900bool test_template_empty_outside_impl() {901 user_lib::vector<int> v;902 v.empty();903 // CHECK-MESSAGES: :[[#@LINE-1]]:3: warning: ignoring the result of 'empty()' [bugprone-standalone-empty]904 return v.empty();905 // no-warning906}907