[clang-tools-extra] [clang-tidy] introduce a must use check (PR #76101)

Tyler Rockwood via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 21 14:33:35 PST 2023


================
@@ -0,0 +1,48 @@
+// RUN: %check_clang_tidy %s misc-must-use %t -- \
+// RUN:       -config="{CheckOptions: [{key: 'misc-must-use.Types', value: '::async::Future'}]}"
+
+namespace async {
+template<typename T>
+class Future {
+public:    
+    T get() {
+        return Pending;
+    }
+private:
+    T Pending;
+};
+
+
+} // namespace async
+
+// Warning is still emitted if there are type aliases.
+namespace a {
+template<typename T>
+using Future = async::Future<T>;
+} // namespace a
+
+void releaseUnits();
+struct Units {
+  ~Units() {
+    releaseUnits();
+  }
+};
+a::Future<Units> acquireUnits();
+
+template<typename T>
+T qux(T Generic) {
+    async::Future<Units> PendingA = acquireUnits();
+    auto PendingB = acquireUnits();
+    // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: variable 'PendingB' must be used [misc-must-use]
+    PendingA.get();
+    return Generic;
+}
+
+int bar(int Num) {
+    a::Future<Units> PendingA = acquireUnits();
+    a::Future<Units> PendingB = acquireUnits(); // not used at all, unused variable not fired because of destructor side effect
+    // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: variable 'PendingB' must be used [misc-must-use]
+    auto Num2 = PendingA.get();
+    auto Num3 = qux(Num);
+    return Num * Num3;
+}
----------------
rockwotj wrote:

Done.

https://github.com/llvm/llvm-project/pull/76101


More information about the cfe-commits mailing list