Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 05f0761

Browse files
committed
Fix new lints
1 parent b1edb3d commit 05f0761

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

fml/memory/ref_counted_unittest.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ TEST(RefCountedTest, NullAssignmentToNull) {
227227
// No-op null assignment using move constructor.
228228
r1 = std::move(r2);
229229
EXPECT_TRUE(r1.get() == nullptr);
230-
EXPECT_TRUE(r2.get() == nullptr);
230+
EXPECT_TRUE(r2.get() == nullptr); // NOLINT(clang-analyzer-cplusplus.Move)
231231
EXPECT_FALSE(r1);
232232
EXPECT_FALSE(r2);
233233

@@ -272,7 +272,7 @@ TEST(RefCountedTest, NonNullAssignmentToNull) {
272272
RefPtr<MyClass> r2;
273273
// Move assignment (to null ref pointer).
274274
r2 = std::move(r1);
275-
EXPECT_TRUE(r1.get() == nullptr);
275+
EXPECT_TRUE(r1.get() == nullptr); // NOLINT(clang-analyzer-cplusplus.Move)
276276
EXPECT_EQ(created, r2.get());
277277
EXPECT_FALSE(r1);
278278
EXPECT_TRUE(r2);
@@ -336,7 +336,7 @@ TEST(RefCountedTest, NullAssignmentToNonNull) {
336336
// Null assignment using move constructor.
337337
r1 = std::move(r2);
338338
EXPECT_TRUE(r1.get() == nullptr);
339-
EXPECT_TRUE(r2.get() == nullptr);
339+
EXPECT_TRUE(r2.get() == nullptr); // NOLINT(clang-analyzer-cplusplus.Move)
340340
EXPECT_FALSE(r1);
341341
EXPECT_FALSE(r2);
342342
EXPECT_TRUE(was_destroyed);
@@ -389,7 +389,7 @@ TEST(RefCountedTest, NonNullAssignmentToNonNull) {
389389
RefPtr<MyClass> r2(MakeRefCounted<MyClass>(nullptr, &was_destroyed2));
390390
// Move assignment (to non-null ref pointer).
391391
r2 = std::move(r1);
392-
EXPECT_TRUE(r1.get() == nullptr);
392+
EXPECT_TRUE(r1.get() == nullptr); // NOLINT(clang-analyzer-cplusplus.Move)
393393
EXPECT_FALSE(r2.get() == nullptr);
394394
EXPECT_FALSE(r1);
395395
EXPECT_TRUE(r2);
@@ -598,13 +598,14 @@ TEST(RefCountedTest, PublicCtorAndDtor) {
598598
TEST(RefCountedTest, DebugChecks) {
599599
{
600600
MyPublicClass* p = new MyPublicClass();
601-
EXPECT_DEATH_IF_SUPPORTED(delete p, "!adoption_required_");
601+
EXPECT_DEATH_IF_SUPPORTED( // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
602+
delete p, "!adoption_required_");
602603
}
603604

604605
{
605606
MyPublicClass* p = new MyPublicClass();
606-
EXPECT_DEATH_IF_SUPPORTED(RefPtr<MyPublicClass> r(p),
607-
"!adoption_required_");
607+
EXPECT_DEATH_IF_SUPPORTED( // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
608+
RefPtr<MyPublicClass> r(p), "!adoption_required_");
608609
}
609610

610611
{

fml/memory/ref_ptr.h

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,42 +66,48 @@ template <typename T>
6666
class RefPtr final {
6767
public:
6868
RefPtr() : ptr_(nullptr) {}
69-
RefPtr(std::nullptr_t) : ptr_(nullptr) {}
69+
RefPtr(std::nullptr_t)
70+
: ptr_(nullptr) {} // NOLINT(google-explicit-constructor)
7071

7172
// Explicit constructor from a plain pointer (to an object that must have
7273
// already been adopted). (Note that in |T::T()|, references to |this| cannot
7374
// be taken, since the object being constructed will not have been adopted
7475
// yet.)
7576
template <typename U>
7677
explicit RefPtr(U* p) : ptr_(p) {
77-
if (ptr_)
78+
if (ptr_) {
7879
ptr_->AddRef();
80+
}
7981
}
8082

8183
// Copy constructor.
8284
RefPtr(const RefPtr<T>& r) : ptr_(r.ptr_) {
83-
if (ptr_)
85+
if (ptr_) {
8486
ptr_->AddRef();
87+
}
8588
}
8689

8790
template <typename U>
88-
RefPtr(const RefPtr<U>& r) : ptr_(r.ptr_) {
89-
if (ptr_)
91+
RefPtr(const RefPtr<U>& r)
92+
: ptr_(r.ptr_) { // NOLINT(google-explicit-constructor)
93+
if (ptr_) {
9094
ptr_->AddRef();
95+
}
9196
}
9297

9398
// Move constructor.
9499
RefPtr(RefPtr<T>&& r) : ptr_(r.ptr_) { r.ptr_ = nullptr; }
95100

96101
template <typename U>
97-
RefPtr(RefPtr<U>&& r) : ptr_(r.ptr_) {
102+
RefPtr(RefPtr<U>&& r) : ptr_(r.ptr_) { // NOLINT(google-explicit-constructor)
98103
r.ptr_ = nullptr;
99104
}
100105

101106
// Destructor.
102107
~RefPtr() {
103-
if (ptr_)
108+
if (ptr_) {
104109
ptr_->Release();
110+
}
105111
}
106112

107113
T* get() const { return ptr_; }
@@ -118,25 +124,34 @@ class RefPtr final {
118124

119125
// Copy assignment.
120126
RefPtr<T>& operator=(const RefPtr<T>& r) {
121-
// Call |AddRef()| first so self-assignments work.
122-
if (r.ptr_)
127+
// Handle self-assignment.
128+
if (r.ptr_ == ptr_) {
129+
return *this;
130+
}
131+
if (r.ptr_) {
123132
r.ptr_->AddRef();
133+
}
124134
T* old_ptr = ptr_;
125135
ptr_ = r.ptr_;
126-
if (old_ptr)
136+
if (old_ptr) {
127137
old_ptr->Release();
138+
}
128139
return *this;
129140
}
130141

131142
template <typename U>
132143
RefPtr<T>& operator=(const RefPtr<U>& r) {
133-
// Call |AddRef()| first so self-assignments work.
134-
if (r.ptr_)
144+
if (reinterpret_cast<T*>(r.ptr_) == ptr_) {
145+
return *this;
146+
}
147+
if (r.ptr_) {
135148
r.ptr_->AddRef();
149+
}
136150
T* old_ptr = ptr_;
137151
ptr_ = r.ptr_;
138-
if (old_ptr)
152+
if (old_ptr) {
139153
old_ptr->Release();
154+
}
140155
return *this;
141156
}
142157

0 commit comments

Comments
 (0)