style: modernize sample code and tame clang-tidy noise

This commit is contained in:
2025-09-23 14:15:40 +08:00
parent b74681a0f5
commit 5b8bf0cdb7
5 changed files with 28 additions and 27 deletions

View File

@@ -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"; \

View File

@@ -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` 或其他設定來符合專案需求。

View File

@@ -10,13 +10,13 @@ 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_;
};
// generate_sequence returns numbers from 0 up to limit (exclusive).
[[nodiscard]] std::vector<int> generate_sequence(int limit);
[[nodiscard]] auto generate_sequence(int limit) -> std::vector<int>;
} // namespace project

View File

@@ -2,14 +2,15 @@
#include <iostream>
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;

View File

@@ -4,11 +4,11 @@
namespace project {
std::string Greeter::greet(const std::string &name) const {
auto Greeter::greet(const std::string &name) const -> std::string {
return base_message_ + ", " + name + "!";
}
std::vector<int> generate_sequence(int limit) {
auto generate_sequence(int limit) -> std::vector<int> {
if (limit < 0) {
limit = 0;
}