summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-09-08 23:09:21 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-09-08 23:09:37 +0200
commit677265461b3c596ab2a88858e74b0970196198fe (patch)
tree40cb1e82e78221dad294b9a521024936c2b716bc
parentbf900719174913774e9559302ac90e11c68ac87a (diff)
Make clang-tidy happy
-rw-r--r--.clang-tidy2
-rw-r--r--src/args.cc27
-rw-r--r--src/args.hh6
-rw-r--r--src/main.cc5
-rw-r--r--src/u16.hh4
-rw-r--r--src/u8.hh4
-rw-r--r--src/umod8.hh4
-rw-r--r--test/args.cc34
8 files changed, 52 insertions, 34 deletions
diff --git a/.clang-tidy b/.clang-tidy
new file mode 100644
index 0000000..0f5830d
--- /dev/null
+++ b/.clang-tidy
@@ -0,0 +1,2 @@
+---
+Checks: 'bugprone-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-misc-non-private-member-variables-in-classes,-modernize-avoid-c-arrays,-modernize-use-trailing-return-type,-readability-magic-numbers,-readability-identifier-length,-readability-braces-around-statements,-readability-function-cognitive-complexity,-readability-redundant-inline-specifier,-readability-implicit-bool-conversion' \ No newline at end of file
diff --git a/src/args.cc b/src/args.cc
index 68f296b..078db1a 100644
--- a/src/args.cc
+++ b/src/args.cc
@@ -1,11 +1,18 @@
#include "args.hh"
+#include <algorithm>
#include <cassert>
+#include <cstddef>
+#include <cstdint>
#include <format>
#include <iostream>
#include <map>
+#include <memory>
#include <optional>
+#include <string>
+#include <string_view>
#include <utility>
+#include <vector>
namespace {
@@ -13,7 +20,7 @@ std::string kEmpty;
class OptionImpl : public Args::OptionArgument {
public:
- enum Type {
+ enum Type : uint8_t {
NoArgument,
RequiredArgument,
OptionalArgument,
@@ -30,11 +37,13 @@ class OptionImpl : public Args::OptionArgument {
const std::string arg;
const std::string help;
- bool is_set() const override { return is_set_; }
+ [[nodiscard]] bool is_set() const override { return is_set_; }
- bool has_argument() const override { return value_.has_value(); }
+ [[nodiscard]] bool has_argument() const override {
+ return value_.has_value();
+ }
- const std::string& argument() const override {
+ [[nodiscard]] const std::string& argument() const override {
if (value_.has_value())
return value_.value();
assert(false);
@@ -117,7 +126,7 @@ class ArgsImpl : public Args {
// "--", no more options signal
if (arguments) {
for (++a; a < argc; ++a)
- arguments->push_back(argv[a]);
+ arguments->emplace_back(argv[a]);
}
break;
}
@@ -193,7 +202,7 @@ class ArgsImpl : public Args {
}
} else {
if (arguments)
- arguments->push_back(argv[a]);
+ arguments->emplace_back(argv[a]);
}
}
return true;
@@ -202,7 +211,7 @@ class ArgsImpl : public Args {
void print_error(std::ostream& out) const override {
if (last_error_.empty()) return;
- out << last_error_ << std::endl;
+ out << last_error_ << '\n';
}
void print_help(std::ostream& out, uint32_t width = 79) const override {
@@ -240,7 +249,7 @@ class ArgsImpl : public Args {
}
need += 2; // margin
- option_need.push_back(need);
+ option_need.emplace_back(need);
if (need <= max_need) {
indent = std::max(indent, need);
}
@@ -309,7 +318,7 @@ class ArgsImpl : public Args {
if (!opt->longname.empty())
long_.emplace(opt->longname, idx);
}
- options_.push_back(std::move(opt));
+ options_.emplace_back(std::move(opt));
}
static inline bool is_whitespace(char c) {
diff --git a/src/args.hh b/src/args.hh
index 75c31b1..2436696 100644
--- a/src/args.hh
+++ b/src/args.hh
@@ -16,7 +16,7 @@ class Args {
public:
virtual ~Option() = default;
- virtual bool is_set() const = 0;
+ [[nodiscard]] virtual bool is_set() const = 0;
protected:
Option() = default;
@@ -26,8 +26,8 @@ class Args {
class OptionArgument : public Option {
public:
- virtual bool has_argument() const = 0;
- virtual const std::string& argument() const = 0;
+ [[nodiscard]] virtual bool has_argument() const = 0;
+ [[nodiscard]] virtual const std::string& argument() const = 0;
};
static std::unique_ptr<Args> create(std::string prgname = std::string());
diff --git a/src/main.cc b/src/main.cc
index 9abed21..27b4854 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -13,7 +13,7 @@ int main(int argc, char** argv) {
auto opt_version = args->option('V', "version", "display version and exit.");
if (!args->run(argc, argv)) {
args->print_error(std::cerr);
- std::cerr << "Try 'jkc --help' for more information." << std::endl;
+ std::cerr << "Try 'jkc --help' for more information.\n";
return 1;
}
if (opt_help->is_set()) {
@@ -21,12 +21,11 @@ int main(int argc, char** argv) {
<< "Java and Kotlin Compiler\n"
<< "\n";
args->print_help(std::cout);
- std::cout << std::flush;
return 0;
}
if (opt_version->is_set()) {
std::cout << "jkc " << VERSION
- << " written by Joel Klinghed <the_jk@spawned.biz>." << std::endl;
+ << " written by Joel Klinghed <the_jk@spawned.biz>.\n";
return 0;
}
return 0;
diff --git a/src/u16.hh b/src/u16.hh
index 6894a84..c23a366 100644
--- a/src/u16.hh
+++ b/src/u16.hh
@@ -1,12 +1,12 @@
#ifndef U16_HH
#define U16_HH
-#include <cstdint>
+#include <cstdint> // IWYU pragma: export
#include <expected>
#include <iterator>
#include <type_traits>
-#include "u.hh"
+#include "u.hh" // IWYU pragma: export
namespace u16 {
diff --git a/src/u8.hh b/src/u8.hh
index 413b156..048af4f 100644
--- a/src/u8.hh
+++ b/src/u8.hh
@@ -1,12 +1,12 @@
#ifndef U8_HH
#define U8_HH
-#include <cstdint>
+#include <cstdint> // IWYU pragma: export
#include <expected>
#include <iterator>
#include <type_traits>
-#include "u.hh"
+#include "u.hh" // IWYU pragma: export
namespace u8 {
diff --git a/src/umod8.hh b/src/umod8.hh
index 8d4fdb2..14406c4 100644
--- a/src/umod8.hh
+++ b/src/umod8.hh
@@ -1,12 +1,12 @@
#ifndef UMOD8_HH
#define UMOD8_HH
-#include <cstdint>
+#include <cstdint> // IWYU pragma: export
#include <expected>
#include <iterator>
#include <type_traits>
-#include "u.hh"
+#include "u.hh" // IWYU pragma: export
namespace umod8 {
diff --git a/test/args.cc b/test/args.cc
index b60a4a4..a43c59c 100644
--- a/test/args.cc
+++ b/test/args.cc
@@ -2,25 +2,33 @@
#include "args.hh"
+#include <cstddef>
+#include <memory>
+#include <sstream>
+#include <string>
+#include <string_view>
+#include <utility>
+#include <vector>
+
#define SETUP_OPTIONS(args) \
- auto short_only = args->option('a', "", "an option"); \
- auto short_long = args->option('b', "bold", "set font style to bold"); \
- auto long_only = args->option("cold", "use if it is cold outside"); \
- auto short_only_req = args->option_argument('d', "", "", "distance"); \
- auto short_long_req = args->option_argument('e', "eat", "FOOD", \
- "what to order, what to eat?"); \
- auto long_only_req = args->option_argument( \
+ auto short_only = (args)->option('a', "", "an option"); \
+ auto short_long = (args)->option('b', "bold", "set font style to bold"); \
+ auto long_only = (args)->option("cold", "use if it is cold outside"); \
+ auto short_only_req = (args)->option_argument('d', "", "", "distance"); \
+ auto short_long_req = (args)->option_argument( \
+ 'e', "eat", "FOOD", "what to order, what to eat?"); \
+ auto long_only_req = (args)->option_argument( \
"form", "", "circle, shape or something else?"); \
- auto short_only_opt = args->option_argument('g', "", "", "", false); \
- auto short_long_opt = args->option_argument('h', "hold", "", "", false); \
- auto long_only_opt = args->option_argument("invert", "", "", false);
+ auto short_only_opt = (args)->option_argument('g', "", "", "", false); \
+ auto short_long_opt = (args)->option_argument('h', "hold", "", "", false); \
+ auto long_only_opt = (args)->option_argument("invert", "", "", false);
namespace {
class Arguments {
public:
- int c() const { return static_cast<int>(str_.size()); };
- char** v() const { return ptr_.get(); }
+ [[nodiscard]] int c() const { return static_cast<int>(str_.size()); };
+ [[nodiscard]] char** v() const { return ptr_.get(); }
explicit Arguments(std::vector<std::string> str)
: str_(std::move(str)) {
@@ -45,7 +53,7 @@ class Builder {
}
Builder& add(std::string str) {
- str_.push_back(std::move(str));
+ str_.emplace_back(std::move(str));
return *this;
}