[llvm] [llvm][ADT] Mark llvm::IntrusiveRefCntPtr with the warn_unused attribute (PR #211795)

Balázs Benics via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 24 07:42:32 PDT 2026


https://github.com/steakhal updated https://github.com/llvm/llvm-project/pull/211795

>From 297041c8e1b0ee34772dc246bab97c2710b7c2d2 Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Fri, 24 Jul 2026 14:47:14 +0100
Subject: [PATCH 1/2] [llvm][ADT] Mark llvm::IntrusiveRefCntPtr with the
 warn_unused attribute

IntrusiveRefCntPtr has non-trivial ctor/dtor, thus unused variables
wouldn't trigger a warning by default. However, they should.
https://clang.llvm.org/docs/AttributeReference.html#warn-unused

Let's mark the class with this attribute get warned about them.

This would have helped catching #211518 and #211517.
Supersedes #211647.
---
 llvm/include/llvm/ADT/IntrusiveRefCntPtr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
index 90349e02014dd..55f80778823ad 100644
--- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
+++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
@@ -170,7 +170,7 @@ template <typename T> struct IntrusiveRefCntPtrInfo {
 /// This class increments its pointee's reference count when it is created, and
 /// decrements its refcount when it's destroyed (or is changed to point to a
 /// different object).
-template <typename T> class IntrusiveRefCntPtr {
+template <typename T> class [[gnu::warn_unused]] IntrusiveRefCntPtr {
   T *Obj = nullptr;
 
 public:

>From 79deb75811d1c7a3af9caa5dad85972c11e1ea15 Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Fri, 24 Jul 2026 15:42:08 +0100
Subject: [PATCH 2/2] Define LLVM_ATTRIBUTE_WARN_UNUSED

---
 llvm/include/llvm/ADT/IntrusiveRefCntPtr.h | 3 ++-
 llvm/include/llvm/Support/Compiler.h       | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
index 55f80778823ad..fe98750632d05 100644
--- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
+++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
@@ -60,6 +60,7 @@
 #ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
 #define LLVM_ADT_INTRUSIVEREFCNTPTR_H
 
+#include "llvm/Support/Compiler.h"
 #include <atomic>
 #include <cassert>
 #include <cstddef>
@@ -170,7 +171,7 @@ template <typename T> struct IntrusiveRefCntPtrInfo {
 /// This class increments its pointee's reference count when it is created, and
 /// decrements its refcount when it's destroyed (or is changed to point to a
 /// different object).
-template <typename T> class [[gnu::warn_unused]] IntrusiveRefCntPtr {
+template <typename T> class LLVM_ATTRIBUTE_WARN_UNUSED IntrusiveRefCntPtr {
   T *Obj = nullptr;
 
 public:
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index 34b328ba1ac8a..753b1e12f721d 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -238,6 +238,12 @@
 #define LLVM_ATTRIBUTE_USED
 #endif
 
+#if __has_attribute(warn_unused)
+#define LLVM_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]]
+#else
+#define LLVM_ATTRIBUTE_WARN_UNUSED
+#endif
+
 // Only enabled for clang:
 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99587
 // GCC may produce "warning: 'retain' attribute ignored" (despite



More information about the llvm-commits mailing list