Skip to content

Commit

Permalink
Forbid goto statement and remove useless struct prefix (#1391)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Apr 15, 2023
1 parent 5db4fad commit 7214f6a
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# refer to https://clang.llvm.org/extra/clang-tidy/checks/list.html
Checks: -*, clang-analyzer-core.*, clang-analyzer-cplusplus.*, clang-analyzer-deadcode.*, clang-analyzer-nullability.*, clang-analyzer-security.*, clang-analyzer-unix.*, clang-analyzer-valist.*, cppcoreguidelines-init-variables, cppcoreguidelines-macro-usage, cppcoreguidelines-interfaces-global-init, cppcoreguidelines-narrowing-conversions, cppcoreguidelines-no-malloc, cppcoreguidelines-prefer-member-initializer, cppcoreguidelines-special-member-functions, cppcoreguidelines-slicing, google-build-explicit-make-pair, google-default-arguments, google-explicit-constructor, modernize-avoid-bind, modernize-loop-convert, modernize-macro-to-enum, modernize-make-shared, modernize-make-unique, modernize-pass-by-value, modernize-redundant-void-arg, modernize-return-braced-init-list, modernize-use-auto, modernize-use-bool-literals, modernize-use-emplace, modernize-use-equals-default, modernize-use-equals-delete, modernize-use-nullptr, modernize-use-override, modernize-use-using, performance-faster-string-find, performance-for-range-copy, performance-implicit-conversion-in-loop, performance-inefficient-algorithm, performance-inefficient-vector-operation, performance-move-const-arg, performance-move-constructor-init, performance-no-automatic-move, performance-trivially-destructible, performance-type-promotion-in-math-fn, performance-unnecessary-copy-initialization, performance-unnecessary-value-param, readability-avoid-const-params-in-decls, readability-const-return-type, readability-convert-member-functions-to-static, readability-make-member-function-const, readability-redundant-access-specifiers, readability-redundant-control-flow, readability-redundant-declaration, readability-redundant-member-init, readability-redundant-string-cstr, readability-redundant-string-init, readability-simplify-boolean-expr, readability-simplify-subscript-expr, readability-string-compare, readability-identifier-naming
Checks: -*, clang-analyzer-core.*, clang-analyzer-cplusplus.*, clang-analyzer-deadcode.*, clang-analyzer-nullability.*, clang-analyzer-security.*, clang-analyzer-unix.*, clang-analyzer-valist.*, cppcoreguidelines-init-variables, cppcoreguidelines-macro-usage, cppcoreguidelines-interfaces-global-init, cppcoreguidelines-narrowing-conversions, cppcoreguidelines-no-malloc, cppcoreguidelines-prefer-member-initializer, cppcoreguidelines-special-member-functions, cppcoreguidelines-slicing, google-build-explicit-make-pair, google-default-arguments, google-explicit-constructor, modernize-avoid-bind, modernize-loop-convert, modernize-macro-to-enum, modernize-make-shared, modernize-make-unique, modernize-pass-by-value, modernize-redundant-void-arg, modernize-return-braced-init-list, modernize-use-auto, modernize-use-bool-literals, modernize-use-emplace, modernize-use-equals-default, modernize-use-equals-delete, modernize-use-nullptr, modernize-use-override, modernize-use-using, performance-faster-string-find, performance-for-range-copy, performance-implicit-conversion-in-loop, performance-inefficient-algorithm, performance-inefficient-vector-operation, performance-move-const-arg, performance-move-constructor-init, performance-no-automatic-move, performance-trivially-destructible, performance-type-promotion-in-math-fn, performance-unnecessary-copy-initialization, performance-unnecessary-value-param, readability-avoid-const-params-in-decls, readability-const-return-type, readability-convert-member-functions-to-static, readability-make-member-function-const, readability-redundant-access-specifiers, readability-redundant-control-flow, readability-redundant-declaration, readability-redundant-member-init, readability-redundant-string-cstr, readability-redundant-string-init, readability-simplify-boolean-expr, readability-simplify-subscript-expr, readability-string-compare, readability-identifier-naming, cppcoreguidelines-avoid-goto

WarningsAsErrors: clang-analyzer-*, -clang-analyzer-security.insecureAPI.rand, google-*, performance-*, cppcoreguidelines-*, modernize-*, readability-*

Expand Down
2 changes: 1 addition & 1 deletion src/cluster/replication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void ReplicationThread::CallbacksStateMachine::EvCallback(bufferevent *bev, void
}
// invoke the read handler (of next step) directly, as the bev might
// have the data already.
goto LOOP_LABEL;
goto LOOP_LABEL; // NOLINT
case CBState::AGAIN:
break;
case CBState::QUIT: // state that can not be retry, or all steps are executed.
Expand Down
2 changes: 1 addition & 1 deletion src/common/cron.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Cron {

private:
std::vector<Scheduler> schedulers_;
struct tm last_tm_ = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr};
tm last_tm_ = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr};

static StatusOr<Scheduler> convertToScheduleTime(const std::string &minute, const std::string &hour,
const std::string &mday, const std::string &month,
Expand Down
5 changes: 2 additions & 3 deletions src/common/io_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ Status SockSetTcpKeepalive(int fd, int interval) {
}

StatusOr<int> SockConnect(const std::string &host, uint32_t port, int conn_timeout, int timeout) {
addrinfo hints, *servinfo = nullptr;
addrinfo hints = {}, *servinfo = nullptr;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

Expand Down Expand Up @@ -151,7 +150,7 @@ StatusOr<int> SockConnect(const std::string &host, uint32_t port, int conn_timeo
}

if (timeout > 0) {
struct timeval tv;
timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
if (setsockopt(*cfd, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char *>(&tv), sizeof(tv)) < 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,20 @@ bool SupervisedSystemd() {
return false;
}

struct sockaddr_un su;
sockaddr_un su;
memset(&su, 0, sizeof(su));
su.sun_family = AF_UNIX;
strncpy(su.sun_path, notify_socket, sizeof(su.sun_path) - 1);
su.sun_path[sizeof(su.sun_path) - 1] = '\0';
if (notify_socket[0] == '@') su.sun_path[0] = '\0';

struct iovec iov;
iovec iov;
memset(&iov, 0, sizeof(iov));
std::string ready = "READY=1";
iov.iov_base = &ready[0];
iov.iov_len = ready.size();

struct msghdr hdr;
msghdr hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.msg_name = &su;
hdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(notify_socket);
Expand Down
4 changes: 2 additions & 2 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ void Server::GetInfo(const std::string &ns, const std::string &section, std::str
}

if (all || section == "cpu") {
struct rusage self_ru;
rusage self_ru;
getrusage(RUSAGE_SELF, &self_ru);
if (section_cnt++) string_stream << "\r\n";
string_stream << "# CPU\r\n";
Expand Down Expand Up @@ -1583,7 +1583,7 @@ void Server::AdjustOpenFilesLimit() {
auto max_clients = static_cast<rlim_t>(config_->maxclients);
auto max_files = max_clients + rocksdb_max_open_file + min_reserved_fds;

struct rlimit limit;
rlimit limit;
if (getrlimit(RLIMIT_NOFILE, &limit) == -1) {
return;
}
Expand Down
21 changes: 8 additions & 13 deletions src/server/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "event2/bufferevent.h"
#include "io_util.h"
#include "scope_exit.h"
#include "thread_util.h"
#include "time_util.h"

Expand Down Expand Up @@ -222,51 +223,45 @@ void Worker::newUnixSocketConnection(evconnlistener *listener, evutil_socket_t f
Status Worker::listenTCP(const std::string &host, uint32_t port, int backlog) {
bool ipv6_used = strchr(host.data(), ':');

struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
addrinfo hints = {};
hints.ai_family = ipv6_used ? AF_INET6 : AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

struct addrinfo *srv_info = nullptr;
addrinfo *srv_info = nullptr;
if (int rv = getaddrinfo(host.data(), std::to_string(port).c_str(), &hints, &srv_info); rv != 0) {
return {Status::NotOK, gai_strerror(rv)};
}
auto exit = MakeScopeExit([srv_info] { freeaddrinfo(srv_info); });

for (auto p = srv_info; p != nullptr; p = p->ai_next) {
int fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (fd == -1) continue;

int sock_opt = 1;
if (ipv6_used && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &sock_opt, sizeof(sock_opt)) == -1) {
goto error;
return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}

if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &sock_opt, sizeof(sock_opt)) < 0) {
goto error;
return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}

// to support multi-thread binding on macOS
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &sock_opt, sizeof(sock_opt)) < 0) {
goto error;
return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}

if (bind(fd, p->ai_addr, p->ai_addrlen)) {
goto error;
return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}

evutil_make_socket_nonblocking(fd);
auto lev = evconnlistener_new(base_, newTCPConnection, this, LEV_OPT_CLOSE_ON_FREE, backlog, fd);
listen_events_.emplace_back(lev);
}

freeaddrinfo(srv_info);
return Status::OK();

error:
freeaddrinfo(srv_info);

return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}

Status Worker::ListenUnixSocket(const std::string &path, int perm, int backlog) {
Expand Down
4 changes: 2 additions & 2 deletions src/stats/stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

Stats::Stats() {
for (int i = 0; i < STATS_METRIC_COUNT; i++) {
struct InstMetric im;
InstMetric im;
im.last_sample_time = 0;
im.last_sample_count = 0;
im.idx = 0;
Expand All @@ -44,7 +44,7 @@ Stats::Stats() {

int64_t Stats::GetMemoryRSS() {
task_t task = MACH_PORT_NULL;
struct task_basic_info t_info;
task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
if (task_for_pid(current_task(), getpid(), &task) != KERN_SUCCESS) return 0;
task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
Expand Down
2 changes: 1 addition & 1 deletion src/storage/redis_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ rocksdb::Status Database::Dump(const Slice &user_key, std::vector<std::string> *
infos->emplace_back(std::to_string(metadata.Is64BitEncoded()));

infos->emplace_back("created_at");
struct timeval created_at = metadata.Time();
timeval created_at = metadata.Time();
std::time_t tm = created_at.tv_sec;
char time_str[25];
if (!std::strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", std::localtime(&tm))) {
Expand Down
4 changes: 1 addition & 3 deletions src/storage/redis_metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,7 @@ int64_t Metadata::TTL() const {

timeval Metadata::Time() const {
auto t = version >> VersionCounterBits;
struct timeval created_at {
static_cast<uint32_t>(t / 1000000), static_cast<int32_t>(t % 1000000)
};
timeval created_at{static_cast<uint32_t>(t / 1000000), static_cast<int32_t>(t % 1000000)};
return created_at;
}

Expand Down

0 comments on commit 7214f6a

Please sign in to comment.