|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +void ModifyString(std::string *s) { |
| 6 | + s->append("!"); |
| 7 | +} |
| 8 | + |
| 9 | +class DataCache { |
| 10 | +public: |
| 11 | + // This member function declaration will also be matched. |
| 12 | + // Okay my code ONLY matched this one. |
| 13 | + // But God that's so much better than nothing. |
| 14 | + const std::vector<int>& get_cached_items() const; |
| 15 | + |
| 16 | +private: |
| 17 | + std::vector<int> items_; |
| 18 | +}; |
| 19 | + |
| 20 | +const std::vector<std::string>& get_global_strings(); |
| 21 | + |
| 22 | +const std::vector<double>& get_default_values() { |
| 23 | + static std::vector<double> defaults = {3.14, 2.71, 1.61}; |
| 24 | + return defaults; |
| 25 | +} |
| 26 | + |
| 27 | +int main() { |
| 28 | + std::cout << "Initializing some_strings.\n"; |
| 29 | + std::vector<std::string> some_strings; |
| 30 | + some_strings.push_back("one"); |
| 31 | + some_strings.push_back("two"); |
| 32 | + some_strings.push_back("three"); |
| 33 | + // Three tools: |
| 34 | + // * suspicious-copy-in-range-loop (as written right now) |
| 35 | + // * performance-for-range-copy with no options |
| 36 | + // * performance-for-range-copy with WarnOnAllAutoCopies enabled |
| 37 | + |
| 38 | + std::cout << "Modify copies; print them as you modify them.\n"; |
| 39 | + // Case 1. |
| 40 | + // These strings are being COPIED and then MODIFIED, so: |
| 41 | + // SHOULD WARN? YES |
| 42 | + // DOES WARN? |
| 43 | + // suspicious-copy-in-range-loop: YES |
| 44 | + // ~ performance-for-range-copy with no options: NO |
| 45 | + // performance-for-range-copy with WarnOnAllAutoCopies enabled: YES |
| 46 | + for (auto x : some_strings) { |
| 47 | + ModifyString(&x); |
| 48 | + std::cout << x << "\n"; |
| 49 | + //std::cout << &x << "\n"; |
| 50 | + } |
| 51 | + |
| 52 | + // Case 2. |
| 53 | + // These strings are being COPIED but left UNMODIFIED, so: |
| 54 | + // SHOULD WARN? NO |
| 55 | + // DOES WARN? |
| 56 | + // ~ suspicious-copy-in-range-loop: YES |
| 57 | + // performance-for-range-copy with no options: NO |
| 58 | + // ~ performance-for-range-copy with WarnOnAllAutoCopies enabled: YES |
| 59 | + for (auto x : some_strings) { |
| 60 | + std::cout << "hi\n"; |
| 61 | + std::cout << x << "\n"; |
| 62 | + //std::cout << &x << "\n"; |
| 63 | + } |
| 64 | + |
| 65 | + // Case 3. |
| 66 | + // These strings are being NEITHER COPIED, NOR MODIFIED, so |
| 67 | + // SHOULD WARN? NO |
| 68 | + // DOES WARN? |
| 69 | + // suspicious-copy-in-range-loop: NO |
| 70 | + // performance-for-range-copy with no options: NO |
| 71 | + // performance-for-range-copy with WarnOnAllAutoCopies enabled: NO |
| 72 | + for (auto& x : some_strings) { |
| 73 | + std::cout << x << "\n"; |
| 74 | + std::cout << &x << "\n"; |
| 75 | + } |
| 76 | + |
| 77 | + // Case 4. |
| 78 | + // These strings are being NOT COPIED but referenced, and MODIFIED, so |
| 79 | + // SHOULD WARN? NO |
| 80 | + // DOES WARN? |
| 81 | + // suspicious-copy-in-range-loop: NO |
| 82 | + // performance-for-range-copy with no options: NO |
| 83 | + // performance-for-range-copy with WarnOnAllAutoCopies enabled: NO |
| 84 | + for (auto& x : some_strings) { |
| 85 | + x = "modd'd!"; |
| 86 | + std::cout << x << "\n"; |
| 87 | + } |
| 88 | +} |
0 commit comments