Commit efbdd604af6df9d757fe6c644cf5cb06b557d2c4

Authored by Christoph Bachhuber
Committed by Henry Schreiner
1 parent 8570ffb5

Refactor clang-tidy (#389)

* Make CI fail with readability-container-size-empty flag

* Make CI fail with cppcoreguidelines-owning-memory flag

* Add all google checks, exclude specific ones

* Apply clang-tidy fixes

* Make timer constructors explicit

* Add check for unscoped namespaces

* Replace unscoped namespace by using-declaration

* Replace unscoped namespace by using-declaration
.clang-tidy
1 -#Checks: '*,-clang-analyzer-alpha.*'  
2 -#Checks: '-*,google-readability-casting,llvm-namespace-comment,performance-unnecessary-value-param,llvm-include-order,misc-throw-by-value-catch-by-reference,readability-container-size-empty,google-runtime-references,modernize*'  
3 -Checks: '-*,llvm-namespace-comment,readability-container-size-empty,misc-throw-by-value-catch-by-reference,modernize*,google-readability-casting' 1 +# Checks that will be implemented in future PRs:
  2 +# performance-unnecessary-value-param, hints to ~110 issues. Be careful with implementing the suggested changes of this one, as auto-fixes may break the code
  3 +
  4 +FormatStyle: file
  5 +
  6 +Checks: '
  7 +-*,
  8 +google-*,
  9 +-google-runtime-int,
  10 +-google-runtime-references,
  11 +llvm-include-order,
  12 +llvm-namespace-comment,
  13 +misc-throw-by-value-catch-by-reference,
  14 +modernize*,
  15 +readability-container-size-empty,
  16 +'
  17 +
  18 +WarningsAsErrors: '
  19 +-*,
  20 +google-*,
  21 +-google-runtime-int,
  22 +-google-runtime-references,
  23 +llvm-include-order,
  24 +llvm-namespace-comment,
  25 +misc-throw-by-value-catch-by-reference,
  26 +modernize*,
  27 +readability-container-size-empty,
  28 +'
  29 +
4 HeaderFilterRegex: '.*hpp' 30 HeaderFilterRegex: '.*hpp'
  31 +
5 CheckOptions: 32 CheckOptions:
6 -- key: readability-braces-around-statements.ShortStatementLines  
7 - value: '1' 33 +- key: google-readability-braces-around-statements.ShortStatementLines
  34 + value: '3'
8 35
examples/enum.cpp
@@ -18,7 +18,7 @@ int main(int argc, char **argv) { @@ -18,7 +18,7 @@ int main(int argc, char **argv) {
18 CLI11_PARSE(app, argc, argv); 18 CLI11_PARSE(app, argc, argv);
19 19
20 // CLI11's built in enum streaming can be used outside CLI11 like this: 20 // CLI11's built in enum streaming can be used outside CLI11 like this:
21 - using namespace CLI::enums; 21 + using CLI::enums::operator<<;
22 std::cout << "Enum received: " << level << std::endl; 22 std::cout << "Enum received: " << level << std::endl;
23 23
24 return 0; 24 return 0;
examples/enum_ostream.cpp
@@ -34,7 +34,7 @@ int main(int argc, char **argv) { @@ -34,7 +34,7 @@ int main(int argc, char **argv) {
34 CLI11_PARSE(app, argc, argv); 34 CLI11_PARSE(app, argc, argv);
35 35
36 // CLI11's built in enum streaming can be used outside CLI11 like this: 36 // CLI11's built in enum streaming can be used outside CLI11 like this:
37 - using namespace CLI::enums; 37 + using CLI::enums::operator<<;
38 std::cout << "Enum received: " << level << std::endl; 38 std::cout << "Enum received: " << level << std::endl;
39 39
40 return 0; 40 return 0;
include/CLI/App.hpp
@@ -2416,10 +2416,11 @@ class App { @@ -2416,10 +2416,11 @@ class App {
2416 2416
2417 /// Count the required remaining positional arguments 2417 /// Count the required remaining positional arguments
2418 bool _has_remaining_positionals() const { 2418 bool _has_remaining_positionals() const {
2419 - for(const Option_p &opt : options_) 2419 + for(const Option_p &opt : options_) {
2420 if(opt->get_positional() && ((static_cast<int>(opt->count()) < opt->get_items_expected_min()))) { 2420 if(opt->get_positional() && ((static_cast<int>(opt->count()) < opt->get_items_expected_min()))) {
2421 return true; 2421 return true;
2422 } 2422 }
  2423 + }
2423 2424
2424 return false; 2425 return false;
2425 } 2426 }
include/CLI/Config.hpp
@@ -314,7 +314,7 @@ ConfigBase::to_config(const App *app, bool default_also, bool write_description, @@ -314,7 +314,7 @@ ConfigBase::to_config(const App *app, bool default_also, bool write_description,
314 } 314 }
315 } 315 }
316 316
317 - for(const App *subcom : subcommands) 317 + for(const App *subcom : subcommands) {
318 if(!subcom->get_name().empty()) { 318 if(!subcom->get_name().empty()) {
319 if(subcom->get_configurable() && app->got_subcommand(subcom)) { 319 if(subcom->get_configurable() && app->got_subcommand(subcom)) {
320 if(!prefix.empty() || app->get_parent() == nullptr) { 320 if(!prefix.empty() || app->get_parent() == nullptr) {
@@ -333,6 +333,7 @@ ConfigBase::to_config(const App *app, bool default_also, bool write_description, @@ -333,6 +333,7 @@ ConfigBase::to_config(const App *app, bool default_also, bool write_description,
333 out << to_config(subcom, default_also, write_description, prefix + subcom->get_name() + "."); 333 out << to_config(subcom, default_also, write_description, prefix + subcom->get_name() + ".");
334 } 334 }
335 } 335 }
  336 + }
336 337
337 return out.str(); 338 return out.str();
338 } 339 }
include/CLI/Error.hpp
@@ -216,16 +216,18 @@ class RequiredError : public ParseError { @@ -216,16 +216,18 @@ class RequiredError : public ParseError {
216 Option(std::size_t min_option, std::size_t max_option, std::size_t used, const std::string &option_list) { 216 Option(std::size_t min_option, std::size_t max_option, std::size_t used, const std::string &option_list) {
217 if((min_option == 1) && (max_option == 1) && (used == 0)) 217 if((min_option == 1) && (max_option == 1) && (used == 0))
218 return RequiredError("Exactly 1 option from [" + option_list + "]"); 218 return RequiredError("Exactly 1 option from [" + option_list + "]");
219 - if((min_option == 1) && (max_option == 1) && (used > 1)) 219 + if((min_option == 1) && (max_option == 1) && (used > 1)) {
220 return RequiredError("Exactly 1 option from [" + option_list + "] is required and " + std::to_string(used) + 220 return RequiredError("Exactly 1 option from [" + option_list + "] is required and " + std::to_string(used) +
221 " were given", 221 " were given",
222 ExitCodes::RequiredError); 222 ExitCodes::RequiredError);
  223 + }
223 if((min_option == 1) && (used == 0)) 224 if((min_option == 1) && (used == 0))
224 return RequiredError("At least 1 option from [" + option_list + "]"); 225 return RequiredError("At least 1 option from [" + option_list + "]");
225 - if(used < min_option) 226 + if(used < min_option) {
226 return RequiredError("Requires at least " + std::to_string(min_option) + " options used and only " + 227 return RequiredError("Requires at least " + std::to_string(min_option) + " options used and only " +
227 std::to_string(used) + "were given from [" + option_list + "]", 228 std::to_string(used) + "were given from [" + option_list + "]",
228 ExitCodes::RequiredError); 229 ExitCodes::RequiredError);
  230 + }
229 if(max_option == 1) 231 if(max_option == 1)
230 return RequiredError("Requires at most 1 options be given from [" + option_list + "]", 232 return RequiredError("Requires at most 1 options be given from [" + option_list + "]",
231 ExitCodes::RequiredError); 233 ExitCodes::RequiredError);
include/CLI/StringTools.hpp
@@ -28,7 +28,7 @@ std::ostream &amp;operator&lt;&lt;(std::ostream &amp;in, const T &amp;item) { @@ -28,7 +28,7 @@ std::ostream &amp;operator&lt;&lt;(std::ostream &amp;in, const T &amp;item) {
28 } // namespace enums 28 } // namespace enums
29 29
30 /// Export to CLI namespace 30 /// Export to CLI namespace
31 -using namespace enums; 31 +using enums::operator<<;
32 32
33 namespace detail { 33 namespace detail {
34 /// a constant defining an expected max vector size defined to be a big number that could be multiplied by 4 and not 34 /// a constant defining an expected max vector size defined to be a big number that could be multiplied by 4 and not
include/CLI/Timer.hpp
@@ -54,7 +54,7 @@ class Timer { @@ -54,7 +54,7 @@ class Timer {
54 54
55 public: 55 public:
56 /// Standard constructor, can set title and print function 56 /// Standard constructor, can set title and print function
57 - Timer(std::string title = "Timer", time_print_t time_print = Simple) 57 + explicit Timer(std::string title = "Timer", time_print_t time_print = Simple)
58 : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {} 58 : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {}
59 59
60 /// Time a function by running it multiple times. Target time is the len to target. 60 /// Time a function by running it multiple times. Target time is the len to target.
@@ -117,7 +117,7 @@ class Timer { @@ -117,7 +117,7 @@ class Timer {
117 class AutoTimer : public Timer { 117 class AutoTimer : public Timer {
118 public: 118 public:
119 /// Reimplementing the constructor is required in GCC 4.7 119 /// Reimplementing the constructor is required in GCC 4.7
120 - AutoTimer(std::string title = "Timer", time_print_t time_print = Simple) : Timer(title, time_print) {} 120 + explicit AutoTimer(std::string title = "Timer", time_print_t time_print = Simple) : Timer(title, time_print) {}
121 // GCC 4.7 does not support using inheriting constructors. 121 // GCC 4.7 does not support using inheriting constructors.
122 122
123 /// This destructor prints the string 123 /// This destructor prints the string