[clang-tools-extra] 28c8e89 - [llvm][ADT] Mark llvm::IntrusiveRefCntPtr with the warn_unused attribute (#211795)

via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 26 06:58:44 PDT 2026


Author: Balázs Benics
Date: 2026-07-26T13:58:39Z
New Revision: 28c8e8931546057c01c517086b99a17230cd4d70

URL: https://github.com/llvm/llvm-project/commit/28c8e8931546057c01c517086b99a17230cd4d70
DIFF: https://github.com/llvm/llvm-project/commit/28c8e8931546057c01c517086b99a17230cd4d70.diff

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

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.

Some tests had to be uplifted because some bots used `-Werror` for those tests.

Added: 
    

Modified: 
    clang-tools-extra/include-cleaner/unittests/TypesTest.cpp
    llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
    llvm/include/llvm/Support/Compiler.h
    llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
    llvm/unittests/Support/VirtualOutputBackendTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/include-cleaner/unittests/TypesTest.cpp b/clang-tools-extra/include-cleaner/unittests/TypesTest.cpp
index 6f7491928fb05..dffc778240a80 100644
--- a/clang-tools-extra/include-cleaner/unittests/TypesTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/TypesTest.cpp
@@ -11,8 +11,6 @@
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/FileSystemOptions.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
-#include "llvm/ADT/IntrusiveRefCntPtr.h"
-#include "llvm/Support/VirtualFileSystem.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
@@ -28,7 +26,6 @@ MATCHER_P(line, N, "") { return arg->Line == (unsigned)N; }
 TEST(RecordedIncludesTest, Match) {
   // We're using synthetic data, but need a FileManager to obtain FileEntry*s.
   // Ensure it doesn't do any actual IO.
-  auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
   FileManager FM(FileSystemOptions{});
   FileEntryRef A = FM.getVirtualFileRef("/path/a", /*Size=*/0, time_t{});
   FileEntryRef B = FM.getVirtualFileRef("/path/b", /*Size=*/0, time_t{});
@@ -48,7 +45,6 @@ TEST(RecordedIncludesTest, Match) {
 }
 
 TEST(RecordedIncludesTest, MatchVerbatim) {
-  auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
   FileManager FM(FileSystemOptions{});
   Includes Inc;
 
@@ -79,8 +75,6 @@ TEST(RecordedIncludesTest, MatchVerbatim) {
 }
 
 TEST(RecordedIncludesTest, MatchVerbatimMixedAbsoluteRelative) {
-  auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
-  FS->setCurrentWorkingDirectory("/working");
   FileManager FM(FileSystemOptions{});
   Includes Inc;
 

diff  --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
index 90349e02014dd..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
 /// 
diff erent object).
-template <typename T> class 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..f9dc2e8d45c4d 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 __attribute__((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

diff  --git a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
index 6da42271764bc..f6a5f587d2719 100644
--- a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
+++ b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
@@ -38,6 +38,8 @@ TYPED_TEST(IntrusiveRefCntPtrTest, RefCountedBaseCopyDoesNotLeak) {
     TypeParam *S2 = new TypeParam(*S1);
     IntrusiveRefCntPtr<TypeParam> R2 = S2;
     EXPECT_EQ(2, NumInstances);
+    (void)R1;
+    (void)R2;
   }
   EXPECT_EQ(0, NumInstances);
 }
@@ -49,6 +51,7 @@ TYPED_TEST(IntrusiveRefCntPtrTest, InteropsWithUniquePtr) {
     IntrusiveRefCntPtr<TypeParam> R1 = std::move(S1);
     EXPECT_EQ(1, NumInstances);
     EXPECT_EQ(S1, nullptr);
+    (void)R1;
   }
   EXPECT_EQ(0, NumInstances);
 }
@@ -91,6 +94,7 @@ TEST(IntrusiveRefCntPtr, UsesTraitsToRetainAndRelease) {
   {
     InterceptRefCounted *I = new InterceptRefCounted(&Released, &Retained);
     IntrusiveRefCntPtr<InterceptRefCounted> R = I;
+    (void)R;
   }
   EXPECT_TRUE(Released);
   EXPECT_TRUE(Retained);

diff  --git a/llvm/unittests/Support/VirtualOutputBackendTest.cpp b/llvm/unittests/Support/VirtualOutputBackendTest.cpp
index 10a0cd9b17a5d..bd085d2d8cbc1 100644
--- a/llvm/unittests/Support/VirtualOutputBackendTest.cpp
+++ b/llvm/unittests/Support/VirtualOutputBackendTest.cpp
@@ -65,7 +65,7 @@ static Error createCustomError() {
 
 TEST(VirtualOutputBackendTest, construct) {
   MockOutputBackendData Data;
-  auto B = createMockBackend(Data);
+  [[maybe_unused]] auto B = createMockBackend(Data);
   EXPECT_EQ(0, Data.Cloned);
   EXPECT_EQ(0, Data.FilesCreated);
 }


        


More information about the cfe-commits mailing list