[clang-tidy] autofix cppcoreguidelines (#1932)
* [clang-tidy] autofix cppcoreguidelines
* better than automation maybe
diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h
index 0b0b6e9..f432205 100644
--- a/include/benchmark/benchmark.h
+++ b/include/benchmark/benchmark.h
@@ -1692,7 +1692,7 @@
CPUInfo const& cpu_info;
SystemInfo const& sys_info;
// The number of chars in the longest benchmark name.
- size_t name_field_width;
+ size_t name_field_width = 0;
static const char* executable_name;
Context();
};
diff --git a/src/benchmark_runner.cc b/src/benchmark_runner.cc
index 388953c..406892a 100644
--- a/src/benchmark_runner.cc
+++ b/src/benchmark_runner.cc
@@ -186,7 +186,7 @@
} // end namespace
BenchTimeType ParseBenchMinTime(const std::string& value) {
- BenchTimeType ret;
+ BenchTimeType ret = {};
if (value.empty()) {
ret.tag = BenchTimeType::TIME;
@@ -195,7 +195,7 @@
}
if (value.back() == 'x') {
- char* p_end;
+ char* p_end = nullptr;
// Reset errno before it's changed by strtol.
errno = 0;
IterationCount num_iters = std::strtol(value.c_str(), &p_end, 10);
@@ -217,7 +217,7 @@
"Eg., `30s` for 30-seconds.";
}
- char* p_end;
+ char* p_end = nullptr;
// Reset errno before it's changed by strtod.
errno = 0;
double min_time = std::strtod(value.c_str(), &p_end);
diff --git a/src/benchmark_runner.h b/src/benchmark_runner.h
index 20a37c1..2e43ff4 100644
--- a/src/benchmark_runner.h
+++ b/src/benchmark_runner.h
@@ -38,7 +38,7 @@
};
struct BENCHMARK_EXPORT BenchTimeType {
- enum { ITERS, TIME } tag;
+ enum { UNSPECIFIED, ITERS, TIME } tag;
union {
IterationCount iters;
double time;
diff --git a/src/string_util.cc b/src/string_util.cc
index e0158a9..e50812e 100644
--- a/src/string_util.cc
+++ b/src/string_util.cc
@@ -105,7 +105,7 @@
std::string ToBinaryStringFullySpecified(double value, int precision,
Counter::OneK one_k) {
std::string mantissa;
- int64_t exponent;
+ int64_t exponent = 0;
ToExponentAndMantissa(value, precision,
one_k == Counter::kIs1024 ? 1024.0 : 1000.0, &mantissa,
&exponent);
@@ -119,7 +119,7 @@
// TODO(ericwf): use std::array for first attempt to avoid one memory
// allocation guess what the size might be
- std::array<char, 256> local_buff;
+ std::array<char, 256> local_buff = {};
// 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation
// in the android-ndk
diff --git a/src/sysinfo.cc b/src/sysinfo.cc
index aeb06f8..89518a9 100644
--- a/src/sysinfo.cc
+++ b/src/sysinfo.cc
@@ -252,7 +252,7 @@
CPUMask mask(benchmark::stoul(part, nullptr, 16));
return static_cast<int>(mask.count());
};
- std::size_t pos;
+ std::size_t pos = 0;
int total = 0;
while ((pos = val.find(',')) != std::string::npos) {
total += CountBits(val.substr(0, pos));
@@ -587,7 +587,7 @@
private:
bool SetAffinity() {
#if defined(BENCHMARK_HAS_PTHREAD_AFFINITY)
- int ret;
+ int ret = 0;
self = pthread_self();
ret = pthread_getaffinity_np(self, sizeof(previous_affinity),
&previous_affinity);
@@ -627,8 +627,8 @@
}
#if defined(BENCHMARK_HAS_PTHREAD_AFFINITY)
- pthread_t self;
- cpu_set_t previous_affinity;
+ pthread_t self{};
+ cpu_set_t previous_affinity{};
#elif defined(BENCHMARK_OS_WINDOWS_WIN32)
HANDLE self;
DWORD_PTR previous_affinity;
@@ -642,7 +642,7 @@
(void)scaling;
#if defined BENCHMARK_OS_LINUX || defined BENCHMARK_OS_CYGWIN
- long freq;
+ long freq = 0;
// If the kernel is exporting the tsc frequency use that. There are issues
// where cpuinfo_max_freq cannot be relied on because the BIOS may be
diff --git a/src/timers.cc b/src/timers.cc
index e0b32de..adaab3a 100644
--- a/src/timers.cc
+++ b/src/timers.cc
@@ -143,7 +143,7 @@
#elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX)
// FIXME We want to use clock_gettime, but its not available in MacOS 10.11.
// See https://github.com/google/benchmark/pull/292
- struct timespec spec;
+ struct timespec spec {};
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &spec) == 0) {
return MakeTime(spec);
}
@@ -200,7 +200,7 @@
if (getrusage(RUSAGE_LWP, &ru) == 0) return MakeTime(ru);
DiagnoseAndExit("getrusage(RUSAGE_LWP, ...) failed");
#elif defined(CLOCK_THREAD_CPUTIME_ID)
- struct timespec ts;
+ struct timespec ts {};
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
return MakeTime(ts);
}
@@ -217,9 +217,9 @@
const std::size_t kTzOffsetLen = 6;
const std::size_t kTimestampLen = 19;
- std::size_t tz_len;
- std::size_t timestamp_len;
- long int offset_minutes;
+ std::size_t tz_len = 0;
+ std::size_t timestamp_len = 0;
+ long int offset_minutes = 0;
char tz_offset_sign = '+';
// tz_offset is set in one of three ways:
// * strftime with %z - This either returns empty or the ISO 8601 time. The
@@ -239,7 +239,7 @@
#if defined(BENCHMARK_OS_WINDOWS)
std::tm* timeinfo_p = ::localtime(&now);
#else
- std::tm timeinfo;
+ std::tm timeinfo{};
std::tm* timeinfo_p = &timeinfo;
::localtime_r(&now, &timeinfo);
#endif
diff --git a/test/benchmark_min_time_flag_iters_test.cc b/test/benchmark_min_time_flag_iters_test.cc
index 56f0fbf..60d512d 100644
--- a/test/benchmark_min_time_flag_iters_test.cc
+++ b/test/benchmark_min_time_flag_iters_test.cc
@@ -13,11 +13,11 @@
class TestReporter : public benchmark::ConsoleReporter {
public:
- virtual bool ReportContext(const Context& context) override {
+ bool ReportContext(const Context& context) override {
return ConsoleReporter::ReportContext(context);
};
- virtual void ReportRuns(const std::vector<Run>& report) override {
+ void ReportRuns(const std::vector<Run>& report) override {
assert(report.size() == 1);
iter_nums_.push_back(report[0].iterations);
ConsoleReporter::ReportRuns(report);
@@ -25,7 +25,7 @@
TestReporter() {}
- virtual ~TestReporter() {}
+ ~TestReporter() override {}
const std::vector<benchmark::IterationCount>& GetIters() const {
return iter_nums_;
diff --git a/test/benchmark_min_time_flag_time_test.cc b/test/benchmark_min_time_flag_time_test.cc
index 0fc1fe9..95a83f5 100644
--- a/test/benchmark_min_time_flag_time_test.cc
+++ b/test/benchmark_min_time_flag_time_test.cc
@@ -19,23 +19,23 @@
class TestReporter : public benchmark::ConsoleReporter {
public:
- virtual bool ReportContext(const Context& context) override {
+ bool ReportContext(const Context& context) override {
return ConsoleReporter::ReportContext(context);
};
- virtual void ReportRuns(const std::vector<Run>& report) override {
+ void ReportRuns(const std::vector<Run>& report) override {
assert(report.size() == 1);
ConsoleReporter::ReportRuns(report);
};
- virtual void ReportRunsConfig(double min_time, bool /* has_explicit_iters */,
- IterationCount /* iters */) override {
+ void ReportRunsConfig(double min_time, bool /* has_explicit_iters */,
+ IterationCount /* iters */) override {
min_times_.push_back(min_time);
}
TestReporter() {}
- virtual ~TestReporter() {}
+ ~TestReporter() override {}
const std::vector<double>& GetMinTimes() const { return min_times_; }
diff --git a/test/filter_test.cc b/test/filter_test.cc
index 1e4dd2e..a931a68 100644
--- a/test/filter_test.cc
+++ b/test/filter_test.cc
@@ -85,7 +85,7 @@
if (argc == 2) {
// Make sure we ran all of the tests
std::stringstream ss(argv[1]);
- int64_t expected_return;
+ int64_t expected_return = 0;
ss >> expected_return;
if (returned_count != expected_return) {
diff --git a/test/output_test_helper.cc b/test/output_test_helper.cc
index e48b465..73ce2bc 100644
--- a/test/output_test_helper.cc
+++ b/test/output_test_helper.cc
@@ -83,7 +83,7 @@
SubMap const& subs = GetSubstitutions();
using SizeT = std::string::size_type;
for (auto const& KV : subs) {
- SizeT pos;
+ SizeT pos = 0;
SizeT next_start = 0;
while ((pos = source.find(KV.first, next_start)) != std::string::npos) {
next_start = pos + KV.second.size();