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 30 HeaderFilterRegex: '.*hpp'
  31 +
5 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 18 CLI11_PARSE(app, argc, argv);
19 19  
20 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 22 std::cout << "Enum received: " << level << std::endl;
23 23  
24 24 return 0;
... ...
examples/enum_ostream.cpp
... ... @@ -34,7 +34,7 @@ int main(int argc, char **argv) {
34 34 CLI11_PARSE(app, argc, argv);
35 35  
36 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 38 std::cout << "Enum received: " << level << std::endl;
39 39  
40 40 return 0;
... ...
include/CLI/App.hpp
... ... @@ -2416,10 +2416,11 @@ class App {
2416 2416  
2417 2417 /// Count the required remaining positional arguments
2418 2418 bool _has_remaining_positionals() const {
2419   - for(const Option_p &opt : options_)
  2419 + for(const Option_p &opt : options_) {
2420 2420 if(opt->get_positional() && ((static_cast<int>(opt->count()) < opt->get_items_expected_min()))) {
2421 2421 return true;
2422 2422 }
  2423 + }
2423 2424  
2424 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 314 }
315 315 }
316 316  
317   - for(const App *subcom : subcommands)
  317 + for(const App *subcom : subcommands) {
318 318 if(!subcom->get_name().empty()) {
319 319 if(subcom->get_configurable() && app->got_subcommand(subcom)) {
320 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 333 out << to_config(subcom, default_also, write_description, prefix + subcom->get_name() + ".");
334 334 }
335 335 }
  336 + }
336 337  
337 338 return out.str();
338 339 }
... ...
include/CLI/Error.hpp
... ... @@ -216,16 +216,18 @@ class RequiredError : public ParseError {
216 216 Option(std::size_t min_option, std::size_t max_option, std::size_t used, const std::string &option_list) {
217 217 if((min_option == 1) && (max_option == 1) && (used == 0))
218 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 220 return RequiredError("Exactly 1 option from [" + option_list + "] is required and " + std::to_string(used) +
221 221 " were given",
222 222 ExitCodes::RequiredError);
  223 + }
223 224 if((min_option == 1) && (used == 0))
224 225 return RequiredError("At least 1 option from [" + option_list + "]");
225   - if(used < min_option)
  226 + if(used < min_option) {
226 227 return RequiredError("Requires at least " + std::to_string(min_option) + " options used and only " +
227 228 std::to_string(used) + "were given from [" + option_list + "]",
228 229 ExitCodes::RequiredError);
  230 + }
229 231 if(max_option == 1)
230 232 return RequiredError("Requires at most 1 options be given from [" + option_list + "]",
231 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 28 } // namespace enums
29 29  
30 30 /// Export to CLI namespace
31   -using namespace enums;
  31 +using enums::operator<<;
32 32  
33 33 namespace detail {
34 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 54  
55 55 public:
56 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 58 : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {}
59 59  
60 60 /// Time a function by running it multiple times. Target time is the len to target.
... ... @@ -117,7 +117,7 @@ class Timer {
117 117 class AutoTimer : public Timer {
118 118 public:
119 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 121 // GCC 4.7 does not support using inheriting constructors.
122 122  
123 123 /// This destructor prints the string
... ...