CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
279 lines
7.6 KiB
C++
279 lines
7.6 KiB
C++
// RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t
|
|
#include <vector>
|
|
#include <memory>
|
|
|
|
#include <cstddef>
|
|
|
|
struct Bar {
|
|
void Do();
|
|
void ConstDo() const;
|
|
};
|
|
struct BarPtr {
|
|
Bar* operator->();
|
|
Bar& operator*();
|
|
Bar* get();
|
|
explicit operator bool() const;
|
|
};
|
|
struct int_ptr {
|
|
int* get();
|
|
int* operator->();
|
|
int& operator*();
|
|
};
|
|
|
|
struct Fail1 {
|
|
Bar* get();
|
|
};
|
|
struct Fail2 {
|
|
Bar* get();
|
|
int* operator->();
|
|
int& operator*();
|
|
};
|
|
|
|
struct PointerWithOverloadedGet {
|
|
int* get();
|
|
template <typename T>
|
|
T* get();
|
|
int* operator->();
|
|
int& operator*();
|
|
};
|
|
|
|
void Positive() {
|
|
BarPtr u;
|
|
// CHECK-FIXES: BarPtr u;
|
|
BarPtr().get()->Do();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant get() call on smart pointer [readability-redundant-smartptr-get]
|
|
// CHECK-MESSAGES: BarPtr().get()->Do();
|
|
// CHECK-FIXES: BarPtr()->Do();
|
|
|
|
u.get()->ConstDo();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant get() call
|
|
// CHECK-MESSAGES: u.get()->ConstDo();
|
|
// CHECK-FIXES: u->ConstDo();
|
|
|
|
Bar& b = *BarPtr().get();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
|
|
// CHECK-MESSAGES: Bar& b = *BarPtr().get();
|
|
// CHECK-FIXES: Bar& b = *BarPtr();
|
|
|
|
Bar& b2 = *std::unique_ptr<Bar>().get();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant get() call
|
|
// CHECK-MESSAGES: Bar& b2 = *std::unique_ptr<Bar>().get();
|
|
// CHECK-FIXES: Bar& b2 = *std::unique_ptr<Bar>();
|
|
|
|
(*BarPtr().get()).ConstDo();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
|
|
// CHECK-MESSAGES: (*BarPtr().get()).ConstDo();
|
|
// CHECK-FIXES: (*BarPtr()).ConstDo();
|
|
|
|
(*std::unique_ptr<Bar>().get()).ConstDo();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
|
|
// CHECK-MESSAGES: (*std::unique_ptr<Bar>().get()).ConstDo();
|
|
// CHECK-FIXES: (*std::unique_ptr<Bar>()).ConstDo();
|
|
|
|
std::unique_ptr<Bar>* up;
|
|
(*up->get()).Do();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
|
|
// CHECK-MESSAGES: (*up->get()).Do();
|
|
// CHECK-FIXES: (**up).Do();
|
|
|
|
int_ptr ip;
|
|
int i = *ip.get();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant get() call
|
|
// CHECK-MESSAGES: int i = *ip.get();
|
|
// CHECK-FIXES: int i = *ip;
|
|
|
|
auto ip2 = ip;
|
|
i = *ip2.get();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
|
|
// CHECK-MESSAGES: i = *ip2.get();
|
|
// CHECK-FIXES: i = *ip2;
|
|
|
|
std::unique_ptr<int> uu;
|
|
std::shared_ptr<double> *ss;
|
|
bool bb = uu.get() == nullptr;
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
|
|
// CHECK-MESSAGES: uu.get() == nullptr;
|
|
// CHECK-FIXES: bool bb = uu == nullptr;
|
|
|
|
if (up->get());
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
|
|
// CHECK-MESSAGES: if (up->get());
|
|
// CHECK-FIXES: if (*up);
|
|
if ((uu.get()));
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
|
|
// CHECK-MESSAGES: if ((uu.get()));
|
|
// CHECK-FIXES: if ((uu));
|
|
bb = !ss->get();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant get() call
|
|
// CHECK-MESSAGES: bb = !ss->get();
|
|
// CHECK-FIXES: bb = !*ss;
|
|
bb = u.get() ? true : false;
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
|
|
// CHECK-MESSAGES: bb = u.get() ? true : false;
|
|
// CHECK-FIXES: bb = u ? true : false;
|
|
|
|
bb = nullptr != ss->get();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: redundant get() call
|
|
// CHECK-MESSAGES: nullptr != ss->get();
|
|
// CHECK-FIXES: bb = nullptr != *ss;
|
|
|
|
i = *PointerWithOverloadedGet().get();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
|
|
// CHECK-MESSAGES: i = *PointerWithOverloadedGet().get();
|
|
// CHECK-FIXES: i = *PointerWithOverloadedGet();
|
|
|
|
bb = std::unique_ptr<int>().get() == NULL;
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
|
|
// CHECK-MESSAGES: bb = std::unique_ptr<int>().get() == NULL;
|
|
// CHECK-FIXES: bb = std::unique_ptr<int>() == NULL;
|
|
bb = ss->get() == NULL;
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
|
|
// CHECK-MESSAGES: bb = ss->get() == NULL;
|
|
// CHECK-FIXES: bb = *ss == NULL;
|
|
|
|
std::unique_ptr<int> x, y;
|
|
if (x.get() == nullptr);
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
|
|
// CHECK-MESSAGES: if (x.get() == nullptr);
|
|
// CHECK-FIXES: if (x == nullptr);
|
|
if (nullptr == y.get());
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: redundant get() call
|
|
// CHECK-MESSAGES: if (nullptr == y.get());
|
|
// CHECK-FIXES: if (nullptr == y);
|
|
if (x.get() == NULL);
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
|
|
// CHECK-MESSAGES: if (x.get() == NULL);
|
|
// CHECK-FIXES: if (x == NULL);
|
|
if (NULL == x.get());
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant get() call
|
|
// CHECK-MESSAGES: if (NULL == x.get());
|
|
// CHECK-FIXES: if (NULL == x);
|
|
}
|
|
|
|
template <typename T>
|
|
void testTemplate() {
|
|
T().get()->Do();
|
|
}
|
|
|
|
template <typename T>
|
|
void testTemplate2() {
|
|
std::unique_ptr<T> up;
|
|
up.get()->Do();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant get() call
|
|
// CHECK-FIXES: up->Do();
|
|
}
|
|
|
|
void instantiate() {
|
|
testTemplate<BarPtr>();
|
|
testTemplate<std::unique_ptr<Bar>>();
|
|
testTemplate<Fail2>();
|
|
|
|
testTemplate2<Bar>();
|
|
}
|
|
|
|
struct S {
|
|
|
|
void foo() {
|
|
m_up.get()->Do();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
|
|
// CHECK-FIXES: m_up->Do();
|
|
m_bp.get()->Do();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
|
|
// CHECK-FIXES: m_bp->Do();
|
|
}
|
|
|
|
std::unique_ptr<Bar> m_up;
|
|
BarPtr m_bp;
|
|
};
|
|
|
|
#define MACRO(p) p.get()
|
|
|
|
void Negative() {
|
|
struct NegPtr {
|
|
int* get();
|
|
int* operator->() {
|
|
return &*this->get();
|
|
}
|
|
int& operator*() {
|
|
return *get();
|
|
}
|
|
};
|
|
|
|
long l = *PointerWithOverloadedGet().get<long>();
|
|
|
|
std::unique_ptr<Bar>* u;
|
|
u->get()->Do();
|
|
|
|
Fail1().get()->Do();
|
|
Fail2().get()->Do();
|
|
const Bar& b = *Fail1().get();
|
|
(*Fail2().get()).Do();
|
|
|
|
int_ptr ip;
|
|
bool bb = ip.get() == nullptr;
|
|
bb = !ip.get();
|
|
bb = ip.get() ? true : false;
|
|
std::unique_ptr<int> x;
|
|
if (MACRO(x) == nullptr)
|
|
;
|
|
}
|
|
|
|
void test_redundant_get() {
|
|
std::vector<std::shared_ptr<int>> v;
|
|
auto f = [](int) {};
|
|
for (auto i = v.begin(); i != v.end(); ++i) {
|
|
f(*i->get());
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
|
|
// CHECK-FIXES: f(**i);
|
|
}
|
|
}
|
|
|
|
struct Inner {
|
|
int a;
|
|
int *getValue() { return &a; }
|
|
};
|
|
|
|
struct Example {
|
|
Inner inner;
|
|
Inner* get() { return &inner; }
|
|
int *getValue() { return inner.getValue(); }
|
|
};
|
|
|
|
void test_redundant_get_with_member() {
|
|
std::vector<std::shared_ptr<Example>> v;
|
|
auto f = [](int) {};
|
|
for (auto i = v.begin(); i != v.end(); ++i) {
|
|
f(*(*i).get()->get()->getValue());
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
|
|
// CHECK-FIXES: f(**i->get()->getValue());
|
|
}
|
|
}
|
|
|
|
void test_smart_ptr_to_array() {
|
|
std::unique_ptr<int[]> i;
|
|
// The array specialization does not have operator*(), so make sure
|
|
// we do not incorrectly suggest sizeof(*i) here.
|
|
// FIXME: alternatively, we could suggest sizeof(i[0])
|
|
auto sz = sizeof(*i.get());
|
|
|
|
std::shared_ptr<Inner[]> s;
|
|
// The array specialization does not have operator->() either
|
|
s.get()->getValue();
|
|
|
|
bool b1 = !s.get();
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant get() call
|
|
// CHECK-FIXES: bool b1 = !s;
|
|
|
|
if (s.get()) {}
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
|
|
// CHECK-FIXES: if (s) {}
|
|
|
|
int x = s.get() ? 1 : 2;
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant get() call
|
|
// CHECK-FIXES: int x = s ? 1 : 2;
|
|
|
|
bool b2 = s.get() == nullptr;
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
|
|
// CHECK-FIXES: bool b2 = s == nullptr;
|
|
}
|