From cfbc94960f4b65ff7fe9d825ad12677dbd164026 Mon Sep 17 00:00:00 2001 From: SunBlack Date: Mon, 16 Jan 2023 13:28:48 +0100 Subject: [PATCH] Fix Clang-Tidy warnings readability-else-after-return (#1528) --- src/benchmark.cc | 27 ++++++++++++++++----------- src/colorprint.cc | 20 ++++++++++---------- src/commandlineflags.cc | 7 ++++--- src/string_util.cc | 6 +++--- test/output_test_helper.cc | 23 ++++++++++++----------- 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/benchmark.cc b/src/benchmark.cc index 539f0de4..8e7408f8 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -410,14 +410,15 @@ std::unique_ptr CreateReporter( typedef std::unique_ptr PtrType; if (name == "console") { return PtrType(new ConsoleReporter(output_opts)); - } else if (name == "json") { - return PtrType(new JSONReporter()); - } else if (name == "csv") { - return PtrType(new CSVReporter()); - } else { - std::cerr << "Unexpected format: '" << name << "'\n"; - std::exit(1); } + if (name == "json") { + return PtrType(new JSONReporter()); + } + if (name == "csv") { + return PtrType(new CSVReporter()); + } + std::cerr << "Unexpected format: '" << name << "'\n"; + std::exit(1); } BENCHMARK_RESTORE_DEPRECATED_WARNING @@ -586,13 +587,17 @@ void PrintUsageAndExit() { void SetDefaultTimeUnitFromFlag(const std::string& time_unit_flag) { if (time_unit_flag == "s") { return SetDefaultTimeUnit(kSecond); - } else if (time_unit_flag == "ms") { + } + if (time_unit_flag == "ms") { return SetDefaultTimeUnit(kMillisecond); - } else if (time_unit_flag == "us") { + } + if (time_unit_flag == "us") { return SetDefaultTimeUnit(kMicrosecond); - } else if (time_unit_flag == "ns") { + } + if (time_unit_flag == "ns") { return SetDefaultTimeUnit(kNanosecond); - } else if (!time_unit_flag.empty()) { + } + if (!time_unit_flag.empty()) { PrintUsageAndExit(); } } diff --git a/src/colorprint.cc b/src/colorprint.cc index 1a000a06..62e9310a 100644 --- a/src/colorprint.cc +++ b/src/colorprint.cc @@ -96,18 +96,18 @@ std::string FormatString(const char* msg, va_list args) { // currently there is no error handling for failure, so this is hack. BM_CHECK(ret >= 0); - if (ret == 0) // handle empty expansion + if (ret == 0) { // handle empty expansion return {}; - else if (static_cast(ret) < size) - return local_buff; - else { - // we did not provide a long enough buffer on our first attempt. - size = static_cast(ret) + 1; // + 1 for the null byte - std::unique_ptr buff(new char[size]); - ret = vsnprintf(buff.get(), size, msg, args); - BM_CHECK(ret > 0 && (static_cast(ret)) < size); - return buff.get(); } + if (static_cast(ret) < size) { + return local_buff; + } + // we did not provide a long enough buffer on our first attempt. + size = static_cast(ret) + 1; // + 1 for the null byte + std::unique_ptr buff(new char[size]); + ret = vsnprintf(buff.get(), size, msg, args); + BM_CHECK(ret > 0 && (static_cast(ret)) < size); + return buff.get(); } std::string FormatString(const char* msg, ...) { diff --git a/src/commandlineflags.cc b/src/commandlineflags.cc index 1f555b27..dcb41495 100644 --- a/src/commandlineflags.cc +++ b/src/commandlineflags.cc @@ -284,14 +284,15 @@ bool IsTruthyFlagValue(const std::string& value) { char v = value[0]; return isalnum(v) && !(v == '0' || v == 'f' || v == 'F' || v == 'n' || v == 'N'); - } else if (!value.empty()) { + } + if (!value.empty()) { std::string value_lower(value); std::transform(value_lower.begin(), value_lower.end(), value_lower.begin(), [](char c) { return static_cast(::tolower(c)); }); return !(value_lower == "false" || value_lower == "no" || value_lower == "off"); - } else - return true; + } + return true; } } // end namespace benchmark diff --git a/src/string_util.cc b/src/string_util.cc index b3196fc2..5e2d24a3 100644 --- a/src/string_util.cc +++ b/src/string_util.cc @@ -94,10 +94,10 @@ std::string ExponentToPrefix(int64_t exponent, bool iec) { const char* array = (exponent > 0 ? (iec ? kBigIECUnits : kBigSIUnits) : kSmallSIUnits); - if (iec) + if (iec) { return array[index] + std::string("i"); - else - return std::string(1, array[index]); + } + return std::string(1, array[index]); } std::string ToBinaryStringFullySpecified(double value, double threshold, diff --git a/test/output_test_helper.cc b/test/output_test_helper.cc index a4765ae0..986c4adb 100644 --- a/test/output_test_helper.cc +++ b/test/output_test_helper.cc @@ -248,9 +248,8 @@ void ResultsChecker::CheckResults(std::stringstream& output) { if (!p.regex->Match(r.name)) { BM_VLOG(2) << p.regex_str << " is not matched by " << r.name << "\n"; continue; - } else { - BM_VLOG(2) << p.regex_str << " is matched by " << r.name << "\n"; } + BM_VLOG(2) << p.regex_str << " is matched by " << r.name << "\n"; BM_VLOG(1) << "Checking results of " << r.name << ": ... \n"; p.fn(r); BM_VLOG(1) << "Checking results of " << r.name << ": OK.\n"; @@ -328,16 +327,18 @@ double Results::GetTime(BenchmarkTime which) const { BM_CHECK(unit); if (*unit == "ns") { return val * 1.e-9; - } else if (*unit == "us") { - return val * 1.e-6; - } else if (*unit == "ms") { - return val * 1.e-3; - } else if (*unit == "s") { - return val; - } else { - BM_CHECK(1 == 0) << "unknown time unit: " << *unit; - return 0; } + if (*unit == "us") { + return val * 1.e-6; + } + if (*unit == "ms") { + return val * 1.e-3; + } + if (*unit == "s") { + return val; + } + BM_CHECK(1 == 0) << "unknown time unit: " << *unit; + return 0; } // ========================================================================= //