Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang-tidy: readability-redundant-smartptr-get does not remove -> (#97964) #98757

Closed
wants to merge 7 commits into from
6 changes: 6 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,12 @@ Changes in existing checks
usages of ``std::string_view::compare``. Added a `StringLikeClasses` option
to detect usages of ``compare`` method in custom string-like classes.

- Improved :doc:`readability-redundant-smartptr-get
<clang-tidy/checks/readability/readability-redundant-smartptr-get>` identify
PiotrZSL marked this conversation as resolved.
Show resolved Hide resolved
and fix redundant calls to the `get()` method on smart pointers. This
improves code readability and efficiency by removing unnecessary
dereferences.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t


#define NULL __null

namespace std {
Expand All @@ -20,6 +21,46 @@ struct shared_ptr {
explicit operator bool() const noexcept;
};

template <typename T>
struct vector {
vector();
bool operator==(const vector<T>& other) const;
bool operator!=(const vector<T>& other) const;
unsigned long size() const;
bool empty() const;

// Basic iterator implementation for testing
struct iterator {
T* ptr;
iterator(T* p) : ptr(p) {}
T& operator*() { return *ptr; }
T* operator->() { return ptr; }
iterator& operator++() {
++ptr;
return *this;
}
bool operator!=(const iterator& other) const { return ptr != other.ptr; }
};

iterator begin();
iterator end();

T* data;
unsigned long sz;
};

template <typename T>
vector<T>::vector() : data(nullptr), sz(0) {}

template <typename T>
typename vector<T>::iterator vector<T>::begin() {
return iterator(data);
}

template <typename T>
typename vector<T>::iterator vector<T>::end() {
return iterator(data + sz);
}
} // namespace std

struct Bar {
Expand Down Expand Up @@ -235,3 +276,13 @@ void Negative() {
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());
PiotrZSL marked this conversation as resolved.
Show resolved Hide resolved
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
// CHECK-FIXES: f(**i);
}
}
1 change: 0 additions & 1 deletion libc/src/errno/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ add_entrypoint_object(
SRCS
libc_errno.cpp
HDRS
errno.h
akshaykumars614 marked this conversation as resolved.
Show resolved Hide resolved
libc_errno.h # Include this
COMPILE_OPTIONS
${full_build_flag}
Expand Down
14 changes: 0 additions & 14 deletions libc/src/errno/errno.h

This file was deleted.

15 changes: 5 additions & 10 deletions libc/src/errno/libc_errno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//

#include "libc_errno.h"
#include "src/errno/errno.h"
#include "src/__support/macros/config.h"

// libc never stores a value; `errno` macro uses get link-time failure.
Expand Down Expand Up @@ -47,9 +46,6 @@ LIBC_ERRNO_MODE_SYSTEM

namespace LIBC_NAMESPACE_DECL {

// Define the global `libc_errno` instance.
Errno libc_errno;

#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_UNDEFINED

void Errno::operator=(int) {}
Expand All @@ -61,9 +57,7 @@ namespace {
LIBC_THREAD_LOCAL int thread_errno;
}

extern "C" {
int *__llvm_libc_errno() { return &thread_errno; }
}
extern "C" int *__llvm_libc_errno() noexcept { return &thread_errno; }

void Errno::operator=(int a) { thread_errno = a; }
Errno::operator int() { return thread_errno; }
Expand All @@ -74,9 +68,7 @@ namespace {
int shared_errno;
}

extern "C" {
int *__llvm_libc_errno() { return &shared_errno; }
}
extern "C" int *__llvm_libc_errno() noexcept { return &shared_errno; }

void Errno::operator=(int a) { shared_errno = a; }
Errno::operator int() { return shared_errno; }
Expand All @@ -93,4 +85,7 @@ Errno::operator int() { return errno; }

#endif

// Define the global `libc_errno` instance.
Errno libc_errno;

} // namespace LIBC_NAMESPACE_DECL
2 changes: 2 additions & 0 deletions libc/src/errno/libc_errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

namespace LIBC_NAMESPACE_DECL {

extern "C" int *__llvm_libc_errno() noexcept;

struct Errno {
void operator=(int);
operator int();
Expand Down
Loading