@@ -325,6 +325,79 @@ class BugPattern:
325325 "Comparing signed `int` with unsigned `.size()` — undefined behavior on overflow." ,
326326 "Cast to `(int)` or use `std::ssize()` (C++20)." ,
327327 "warning" , ["C++" ]),
328+ BugPattern ("Void Main" , r"\bvoid\s+main\s*\(" ,
329+ "`void main()` is non-standard C++ and results in a compilation error." ,
330+ "Use `int main()` and return 0 at the end." ,
331+ "error" , ["C++" ]),
332+ BugPattern ("Single Quotes for String" , r"'[^'\\]{2,}'" ,
333+ "Single quotes are used for strings. In C++, single quotes are strictly for single characters." ,
334+ "Use double quotes `\" ...\" ` for string literals." ,
335+ "error" , ["C++" ]),
336+ BugPattern ("Missing Semicolon" , r"^(?!.*\b(if|for|while|switch|catch)\b)(?!.*[{}#])(?!^\s*(int|float|double|char|long|short|bool|string|void)\s+\w+\s*\([^)]*\)\s*$).*\b(cout|cin|return|int|float|double|char|long|short|bool|string)\b[^;]*[^\s;]\s*$" ,
337+ "Missing semicolon at the end of the statement." ,
338+ "Add a semicolon `;` at the end of the line." ,
339+ "error" , ["C++" ]),
340+
341+ BugPattern ("Incomplete Assignment" , r"=\s*$" ,
342+ "Statement ends abruptly with an assignment operator." ,
343+ "Provide a value for the assignment." ,
344+ "error" , ["C++" , "Java" , "Python" , "JavaScript" , "TypeScript" ]),
345+ BugPattern ("Semicolon After Loop" , r"\b(for|while)\s*\([^)]*\)\s*;" ,
346+ "Semicolon immediately after loop condition creates an empty loop body." ,
347+ "Remove the semicolon so the loop executes the intended block." ,
348+ "error" , ["C++" , "Java" , "JavaScript" , "TypeScript" ]),
349+ BugPattern ("Type Mismatch: String to Int" , r"\b(int|long|short)\s+[a-zA-Z_]\w*\s*=\s*\"[^\"]*\"" ,
350+ "Attempting to assign a string literal to an integer variable." ,
351+ "Use `std::string` for strings, or parse the string using `std::stoi`." ,
352+ "error" , ["C++" , "Java" ]),
353+ BugPattern ("Uninitialized Variable Risk" , r"^\s*(int|float|double|char|long|short)\s+[a-zA-Z_]\w*\s*;\s*$" ,
354+ "Variable is declared without an initial value. Using it before assignment causes undefined behavior." ,
355+ "Initialize the variable upon declaration (e.g., `= 0;`)." ,
356+ "warning" , ["C++" ]),
357+ BugPattern ("Float Equality" , r"==\s*\d+\.\d+" ,
358+ "Directly comparing floating point numbers with `==` is unsafe due to precision issues." ,
359+ "Compare the absolute difference with an epsilon value (e.g., `abs(a - b) < 1e-9`)." ,
360+ "warning" , ["C++" , "Java" , "Python" , "JavaScript" ]),
361+ BugPattern ("Variable Length Array" , r"\b(int|float|double|char|long|short)\s+[a-zA-Z_]\w*\s*\[\s*[a-zA-Z_]\w*\s*\]\s*;" ,
362+ "Using a variable to define an array size (VLA) is not standard C++ and fails on some compilers." ,
363+ "Use `std::vector` for dynamically sized arrays." ,
364+ "error" , ["C++" ]),
365+ BugPattern ("Negative Array Index" , r"\[\s*-\s*\d+\s*\]" ,
366+ "Hardcoded negative index detected. In C++ this accesses memory out of bounds." ,
367+ "Ensure array indices are 0 or greater." ,
368+ "error" , ["C++" , "Java" , "JavaScript" , "TypeScript" ]),
369+ BugPattern ("C-Style Array" , r"\b(int|float|double|char|long|short)\s+[a-zA-Z_]\w*\s*\[\s*\d+\s*\]\s*;" ,
370+ "Raw C-style arrays do not carry their size and unsafely decay to pointers." ,
371+ "Use `std::array<T, N>` for fixed-size arrays." ,
372+ "info" , ["C++" ]),
373+ BugPattern ("Vector Pass by Value" , r"\b\w+\s*\(\s*std::vector\s*<\s*[\w:]+\s*>\s+\w+\s*[,)]" ,
374+ "Passing a `std::vector` by value creates a full, expensive copy." ,
375+ "Pass by const reference (e.g., `const std::vector<T>&`) unless you need to mutate a copy." ,
376+ "warning" , ["C++" ]),
377+ BugPattern ("Vector Unsigned Underflow" , r"\.size\(\)\s*-\s*1" ,
378+ "Vector `.size()` is unsigned. If empty, subtracting 1 causes an underflow to a huge number." ,
379+ "Always check `.empty()` first, or cast size to a signed integer." ,
380+ "error" , ["C++" ]),
381+ BugPattern ("malloc in C++" , r"\bmalloc\s*\(" ,
382+ "C-style `malloc` allocates memory but does not call C++ constructors." ,
383+ "Use `new` or `std::make_unique` instead." ,
384+ "error" , ["C++" ]),
385+ BugPattern ("Dangling Pointer Return" , r"return\s+&\s*\w+\s*;" ,
386+ "Returning the address of a local variable creates a dangling pointer." ,
387+ "Return by value, or allocate on the heap and return a smart pointer." ,
388+ "error" , ["C++" ]),
389+ BugPattern ("Missing Hash in Include" , r"^\s*include\s*[<\"]" ,
390+ "Preprocessor directives must start with a `#`." ,
391+ "Add a `#` at the beginning of the line (e.g., `#include`)." ,
392+ "error" , ["C++" ]),
393+ BugPattern ("Semicolon in Condition" , r"\b(if|while|switch)\s*\([^)]*;\s*\)" ,
394+ "Condition blocks (if, while, switch) should not contain semicolons." ,
395+ "Remove the semicolon from inside the parentheses." ,
396+ "error" , ["C++" , "Java" , "JavaScript" , "TypeScript" ]),
397+ BugPattern ("Malformed For-Loop" , r"\bfor\s*\([^;:]*(?:;[^;:]*)?\)" ,
398+ "A traditional for-loop must contain exactly two semicolons." ,
399+ "Ensure you have two semicolons separating the initialization, condition, and increment statements." ,
400+ "error" , ["C++" , "Java" , "JavaScript" , "TypeScript" ]),
328401
329402 # ── PHP ──
330403 BugPattern ("PHP MySQL Deprecated" , r"\bmysql_\w+\s*\(" ,
@@ -601,6 +674,17 @@ def run_suggestions(code: str, language: str) -> dict:
601674 "priority" : "medium" ,
602675 })
603676
677+ # std::endl Performance (only if in a file with loops)
678+ if language == "C++" :
679+ if re .search (r"<<\s*(std::)?endl\b" , code ) and re .search (r"\b(for|while)\b" , code ):
680+ suggestions .append ({
681+ "category" : "Performance" ,
682+ "description" : "Code contains both a loop and `std::endl`. If `std::endl` is used inside the loop, it flushes the buffer on every iteration, severely degrading performance." ,
683+ "example" : "std::cout << value << '\\ n';" ,
684+ "priority" : "medium" ,
685+ })
686+
687+ # Score
604688 # Score calculation
605689 deductions = sum ({"high" : 15 , "medium" : 7 , "low" : 3 }.get (s ["priority" ], 5 ) for s in suggestions )
606690 score = max (0 , min (100 , 100 - deductions ))
0 commit comments