-
Notifications
You must be signed in to change notification settings - Fork 8
Preview/Clang-Tidy #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| Checks: >- | ||
| -*, | ||
| clang-analyzer-*, | ||
| bugprone-*, | ||
| modernize-use-nullptr, | ||
| modernize-use-override, | ||
| cppcoreguidelines-owning-memory, | ||
| cppcoreguidelines-pro-type-member-init | ||
| WarningsAsErrors: >- | ||
| clang-analyzer-*, | ||
| bugprone-*, | ||
| modernize-use-nullptr, | ||
| modernize-use-override, | ||
| cppcoreguidelines-owning-memory, | ||
| cppcoreguidelines-pro-type-member-init | ||
| FormatStyle: none |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| reviews: | ||
| tools: | ||
| clang: | ||
| enabled: true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [ | ||
| { | ||
| "directory": "/home/jailuser/git", | ||
| "file": "test.cpp", | ||
| "command": "clang++ -std=c++17 -I. -c test.cpp" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,48 @@ | ||||||||||||||||||||
| #include <cstdio> | ||||||||||||||||||||
| #include <cstdlib> | ||||||||||||||||||||
| #include <cstring> | ||||||||||||||||||||
| #include <iostream> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| class Shape { | ||||||||||||||||||||
| public: | ||||||||||||||||||||
| Shape() {} | ||||||||||||||||||||
| virtual void draw() {} | ||||||||||||||||||||
| ~Shape() {} | ||||||||||||||||||||
| private: | ||||||||||||||||||||
| int size; | ||||||||||||||||||||
| int color; | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
Comment on lines
+6
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-virtual destructor with virtual methods causes undefined behavior.
Additionally, the constructor on line 8 does not initialize the Proposed fix class Shape {
public:
- Shape() {}
+ Shape() : size(0), color(0) {}
virtual void draw() {}
- ~Shape() {}
+ virtual ~Shape() = default;
private:
int size;
int color;
};🧰 Tools🪛 Clang (14.0.6)[error] 8-8: constructor does not initialize these fields: size, color (cppcoreguidelines-pro-type-member-init,-warnings-as-errors) 🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| class Circle : public Shape { | ||||||||||||||||||||
| public: | ||||||||||||||||||||
| Circle() {} | ||||||||||||||||||||
| void draw() { std::cout << "circle" << std::endl; } | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cd / && find . -name "test.cpp" -type f 2>/dev/null | head -5Repository: coderabbitai/coderabbit-pr-review Length of output: 106 🏁 Script executed: cat -n ./home/jailuser/git/test.cppRepository: coderabbitai/coderabbit-pr-review Length of output: 138 🏁 Script executed: git ls-files | grep test.cppRepository: coderabbitai/coderabbit-pr-review Length of output: 86 🏁 Script executed: cat -n test.cppRepository: coderabbitai/coderabbit-pr-review Length of output: 1137 Add Line 19 overrides the Proposed fix- void draw() { std::cout << "circle" << std::endl; }
+ void draw() override { std::cout << "circle" << std::endl; }📝 Committable suggestion
Suggested change
🧰 Tools🪛 Clang (14.0.6)[error] 19-19: annotate this function with 'override' or (rarely) 'final' (modernize-use-override,-warnings-as-errors) 🤖 Prompt for AI Agents |
||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| void nullDeref(int* p) { | ||||||||||||||||||||
| if (p == NULL) { | ||||||||||||||||||||
| *p = 5; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Find test.cpp in the repository
find . -name "test.cpp" -type fRepository: coderabbitai/coderabbit-pr-review Length of output: 88 🏁 Script executed: # Read test.cpp to verify lines 22-25
cat -n test.cpp | head -30Repository: coderabbitai/coderabbit-pr-review Length of output: 754 Null check is inverted and dereferences null At line 23, when Proposed fix void nullDeref(int* p) {
- if (p == NULL) {
+ if (p != nullptr) {
*p = 5;
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| int divide(int numerator, int denominator) { | ||||||||||||||||||||
| return numerator / denominator; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cat test.cppRepository: coderabbitai/coderabbit-pr-review Length of output: 801 Deterministic division-by-zero path
Proposed fix+#include <stdexcept>
int divide(int numerator, int denominator) {
+ if (denominator == 0) {
+ throw std::invalid_argument("denominator must not be zero");
+ }
return numerator / denominator;
}
@@
- int x = divide(100, 0);
+ int x = divide(100, 5);🧰 Tools🪛 Cppcheck (2.20.0)[error] 29-29: Division by zero. (zerodiv) 🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| int main() { | ||||||||||||||||||||
| int uninitialized; | ||||||||||||||||||||
| int result = uninitialized + 7; | ||||||||||||||||||||
|
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reading uninitialized local variable is undefined behavior.
Proposed fix- int uninitialized;
- int result = uninitialized + 7;
+ int initialized = 0;
+ int result = initialized + 7;📝 Committable suggestion
Suggested change
🧰 Tools🪛 Cppcheck (2.20.0)[error] 34-34: Uninitialized variable (uninitvar) 🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| int x = divide(100, 0); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| int* p = NULL; | ||||||||||||||||||||
| nullDeref(p); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Shape* s = new Circle(); | ||||||||||||||||||||
| s->draw(); | ||||||||||||||||||||
|
Comment on lines
+41
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Memory leak: raw
Proposed fix+#include <memory>
// ...
- Shape* s = new Circle();
- s->draw();
+ auto s = std::make_unique<Circle>();
+ s->draw();📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| const char* fmt = "%s"; | ||||||||||||||||||||
| printf(fmt); | ||||||||||||||||||||
|
Comment on lines
+44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: find . -name "test.cpp" -type fRepository: coderabbitai/coderabbit-pr-review Length of output: 88 🏁 Script executed: cat -n ./test.cppRepository: coderabbitai/coderabbit-pr-review Length of output: 1137
Line 45 passes a Proposed fix- printf(fmt);
+ std::printf("%s", fmt);📝 Committable suggestion
Suggested change
🧰 Tools🪛 Cppcheck (2.20.0)[error] 45-45: printf format string requires 1 parameter but only 0 are given. (wrongPrintfScanfArgNum) 🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| return result + x; | ||||||||||||||||||||
| } | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: coderabbitai/coderabbit-pr-review
Length of output: 179
🏁 Script executed:
Repository: coderabbitai/coderabbit-pr-review
Length of output: 413
🏁 Script executed:
Repository: coderabbitai/coderabbit-pr-review
Length of output: 197
🏁 Script executed:
Repository: coderabbitai/coderabbit-pr-review
Length of output: 114
Compilation fails before analysis: missing standard include paths
The compilation command in
compile_commands.jsonfortest.cppomits system include paths. Line 1's#include <cstdio>fails with'cstdio' file not found, preventing clang-tidy from analyzing this file.Fix
compile_commands.jsonto include proper system include paths (e.g., via-isystemor by using a compiler driver that provides them automatically).🧰 Tools
🪛 Clang (14.0.6)
[error] 1-1: 'cstdio' file not found
(clang-diagnostic-error)
🤖 Prompt for AI Agents