| 1 | // RUN: %check_clang_tidy -std=c++17-or-later %s modernize-use-scoped-lock %t -- \ |
| 2 | // RUN: -config="{CheckOptions: {modernize-use-scoped-lock.WarnOnSingleLocks: false}}" \ |
| 3 | // RUN: -- -isystem %clang_tidy_headers -fno-delayed-template-parsing |
| 4 | |
| 5 | #include <mutex> |
| 6 | |
| 7 | void Positive() { |
| 8 | std::mutex m; |
| 9 | |
| 10 | { |
| 11 | std::lock_guard<std::mutex> l1(m); |
| 12 | std::lock_guard<std::mutex> l2(m); |
| 13 | // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use single 'std::scoped_lock' instead of multiple 'std::lock_guard' |
| 14 | // CHECK-MESSAGES: :[[@LINE-2]]:33: note: additional 'std::lock_guard' declared here |
| 15 | } |
| 16 | |
| 17 | { |
| 18 | std::lock_guard<std::mutex> l1(m), l2(m), l3(m); |
| 19 | std::lock_guard<std::mutex> l4(m); |
| 20 | // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use single 'std::scoped_lock' instead of multiple 'std::lock_guard' |
| 21 | // CHECK-MESSAGES: :[[@LINE-3]]:40: note: additional 'std::lock_guard' declared here |
| 22 | // CHECK-MESSAGES: :[[@LINE-4]]:47: note: additional 'std::lock_guard' declared here |
| 23 | // CHECK-MESSAGES: :[[@LINE-4]]:33: note: additional 'std::lock_guard' declared here |
| 24 | } |
| 25 | |
| 26 | { |
| 27 | std::lock(l1&: m, l2&: m); |
| 28 | std::lock_guard<std::mutex> l1(m, std::adopt_lock); |
| 29 | std::lock_guard<std::mutex> l2(m, std::adopt_lock); |
| 30 | // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use single 'std::scoped_lock' instead of multiple 'std::lock_guard' |
| 31 | // CHECK-MESSAGES: :[[@LINE-2]]:33: note: additional 'std::lock_guard' declared here |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void Negative() { |
| 36 | std::mutex m; |
| 37 | { |
| 38 | std::lock_guard<std::mutex> l(m); |
| 39 | } |
| 40 | |
| 41 | { |
| 42 | std::lock_guard<std::mutex> l(m, std::adopt_lock); |
| 43 | } |
| 44 | |
| 45 | { |
| 46 | std::lock_guard<std::mutex> l3(m); |
| 47 | int a = 0; |
| 48 | std::lock_guard<std::mutex> l4(m, std::adopt_lock); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void PositiveInsideArg(std::mutex &m1, std::mutex &m2, std::mutex &m3) { |
| 53 | std::lock_guard<std::mutex> l1(m1); |
| 54 | std::lock_guard<std::mutex> l2(m2); |
| 55 | // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use single 'std::scoped_lock' instead of multiple 'std::lock_guard' |
| 56 | // CHECK-MESSAGES: :[[@LINE-2]]:31: note: additional 'std::lock_guard' declared here |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void NegativeInsideArg(std::mutex &m1, std::mutex &m2, std::mutex &m3) { |
| 61 | std::lock_guard<std::mutex> l3(m3); |
| 62 | } |
| 63 | |
| 64 | template <typename T> |
| 65 | void PositiveTemplated() { |
| 66 | std::mutex m1, m2; |
| 67 | |
| 68 | std::lock_guard<std::mutex> l1(m1); |
| 69 | std::lock_guard<std::mutex> l2(m2); |
| 70 | // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use single 'std::scoped_lock' instead of multiple 'std::lock_guard' |
| 71 | // CHECK-MESSAGES: :[[@LINE-2]]:31: note: additional 'std::lock_guard' declared here |
| 72 | } |
| 73 | |
| 74 | template <typename T> |
| 75 | void NegativeTemplated() { |
| 76 | std::mutex m1, m2, m3; |
| 77 | std::lock_guard<std::mutex> l(m1); |
| 78 | } |
| 79 | |
| 80 | template <typename Mutex> |
| 81 | void PositiveTemplatedMutex() { |
| 82 | Mutex m1, m2; |
| 83 | |
| 84 | std::lock_guard<Mutex> l1(m1); |
| 85 | std::lock_guard<Mutex> l2(m2); |
| 86 | // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use single 'std::scoped_lock' instead of multiple 'std::lock_guard' |
| 87 | // CHECK-MESSAGES: :[[@LINE-2]]:26: note: additional 'std::lock_guard' declared here |
| 88 | } |
| 89 | |
| 90 | template <typename Mutex> |
| 91 | void NegativeTemplatedMutex() { |
| 92 | Mutex m1; |
| 93 | std::lock_guard<Mutex> l(m1); |
| 94 | } |
| 95 | |
| 96 | struct NegativeClass { |
| 97 | void Negative() { |
| 98 | std::lock_guard<std::mutex> l(m1); |
| 99 | } |
| 100 | |
| 101 | std::mutex m1; |
| 102 | }; |
| 103 | |