From 5b8bf0cdb720dc9182d5aae1618a0d2a44b6df35 Mon Sep 17 00:00:00 2001 From: MH Hung Date: Tue, 23 Sep 2025 14:15:40 +0800 Subject: [PATCH] style: modernize sample code and tame clang-tidy noise --- Makefile | 2 +- README.md | 2 +- include/project/sample_library.hpp | 22 +++++++++++----------- src/app/main.cpp | 5 +++-- src/lib/sample_library.cpp | 24 ++++++++++++------------ 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index df333be..cc90c88 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ tidy: @if [ -n "$(strip $(SRCS))" ]; then \ for src in $(SRCS); do \ echo "running clang-tidy on $$src"; \ - $(CLANG_TIDY) $$src -- $(CPPFLAGS) $(CXXFLAGS); \ + $(CLANG_TIDY) -quiet $$src -- $(CPPFLAGS) $(CXXFLAGS); \ done; \ else \ echo "warning: no source files found for clang-tidy"; \ diff --git a/README.md b/README.md index d9bef92..69ab57e 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ CXX=clang++ CXXFLAGS="-std=c++20 -O3" make `.clang-tidy` 啟用了一些常用的檢查(`bugprone-*`、`clang-analyzer-*`、`modernize-*` 等)。可使用 `make tidy` 或 `CLANG_TIDY=/path/to/clang-tidy make tidy` 對 `src/*.cpp` 逐一檢查;若要檢查特定檔案,可執行: ```bash -clang-tidy src/app/main.cpp -- -Iinclude +clang-tidy -quiet src/app/main.cpp -- -Iinclude ``` 可視需求調整 `Checks`、`HeaderFilterRegex` 或其他設定來符合專案需求。 diff --git a/include/project/sample_library.hpp b/include/project/sample_library.hpp index 0b6027a..516f135 100644 --- a/include/project/sample_library.hpp +++ b/include/project/sample_library.hpp @@ -5,18 +5,18 @@ namespace project { -// Greeter builds a greeting message for a given name. -class Greeter { -public: - explicit Greeter(std::string base = "Hello") : base_message_(std::move(base)) {} + // Greeter builds a greeting message for a given name. + class Greeter { + public: + explicit Greeter(std::string base = "Hello") : base_message_(std::move(base)) {} - [[nodiscard]] std::string greet(const std::string &name) const; + [[nodiscard]] auto greet(const std::string &name) const -> std::string; -private: - std::string base_message_; -}; + private: + std::string base_message_; + }; -// generate_sequence returns numbers from 0 up to limit (exclusive). -[[nodiscard]] std::vector generate_sequence(int limit); + // generate_sequence returns numbers from 0 up to limit (exclusive). +[[nodiscard]] auto generate_sequence(int limit) -> std::vector; -} // namespace project +} // namespace project diff --git a/src/app/main.cpp b/src/app/main.cpp index 2de19a5..dd9e10c 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -2,14 +2,15 @@ #include -int main() { +auto main() -> int { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); const project::Greeter greeter{"Welcome"}; std::cout << greeter.greet("Coding World") << '\n'; - auto sequence = project::generate_sequence(5); + constexpr int kSampleSequenceLength = 5; + auto sequence = project::generate_sequence(kSampleSequenceLength); std::cout << "Sample sequence:"; for (int value : sequence) { std::cout << ' ' << value; diff --git a/src/lib/sample_library.cpp b/src/lib/sample_library.cpp index 87b6c8f..8b6ef25 100644 --- a/src/lib/sample_library.cpp +++ b/src/lib/sample_library.cpp @@ -4,18 +4,18 @@ namespace project { -std::string Greeter::greet(const std::string &name) const { - return base_message_ + ", " + name + "!"; -} - -std::vector generate_sequence(int limit) { - if (limit < 0) { - limit = 0; +auto Greeter::greet(const std::string &name) const -> std::string { + return base_message_ + ", " + name + "!"; } - std::vector result(static_cast(limit)); - std::iota(result.begin(), result.end(), 0); - return result; -} +auto generate_sequence(int limit) -> std::vector { + if (limit < 0) { + limit = 0; + } -} // namespace project + std::vector result(static_cast(limit)); + std::iota(result.begin(), result.end(), 0); + return result; + } + +} // namespace project