style: modernize sample code and tame clang-tidy noise
This commit is contained in:
2
Makefile
2
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"; \
|
||||
|
@@ -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` 或其他設定來符合專案需求。
|
||||
|
@@ -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
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user