Code quality rules
Finds bugs, error-handling gaps, missing tests and maintainability debt in individual files and in the lines a change touches.
- Agent id:
code_quality - Rules: 17 (source:
code_quality.yaml) - Deterministic checks:
metric_thresholds,duplicate_blocks
CQ-001: Logic error or unhandled edge case
- Severity: high
- Languages: all
Read each changed function for conditions that are inverted, off by one, use the wrong variable, compare with is or == incorrectly, or skip an edge case the inputs allow (empty collection, zero, negative number, missing key, None, first or last element). Report only a case you can name with a concrete input that produces the wrong result.
CQ-002: Exception swallowed
- Severity: high
- Languages: all
- References: CWE-390, CWE-396
Look for except: or except Exception: pass, empty catch {} blocks, and handlers that log and continue in a state that is now wrong (partial write, missing value used later). A narrow exception caught on purpose with a comment or a fallback value that is correct for that case is not a violation.
CQ-003: Resource not released
- Severity: medium
- Languages: all
- References: CWE-772, CWE-404
Look for files, sockets, database connections, cursors, locks or temporary directories opened without with, try/finally, using, defer or an equivalent, so an exception leaves them open. Objects whose lifetime is the whole process, and resources handed to a caller that clearly owns closing them, are not violations.
CQ-004: High cyclomatic complexity
- Severity: medium
- Languages: all
- References: CWE-1121
Look for functions with cyclomatic complexity above 15, as reported by the metrics check, and propose one concrete split (extract the validation branch, replace a condition chain with a lookup table, return early). A flat switch or match that maps many cases to one-line results counts high but is easy to read; report it as low.
CQ-005: Deep nesting
- Severity: low
- Languages: all
- References: CWE-1124
Look for code nested more than four levels deep in loops, conditions and try blocks, and suggest guard clauses or extracting the inner block into a named function. Nesting that comes only from a class, a function and one context manager is normal.
CQ-006: Long function
- Severity: low
- Languages: all
Look for functions longer than 80 lines that do several separate steps, and name the steps that should become functions. A long function that is one linear sequence of simple statements, such as a table of route registrations or a data literal, is not a violation.
CQ-007: Duplicated block
- Severity: low
- Languages: all
- References: CWE-1041
Look for blocks of five or more lines repeated within a file or across files with only names or literals changed, and suggest the shared function or loop. Test cases that repeat a short arrange-act-assert shape on purpose for readability are not violations.
CQ-008: Changed behavior without a test
- Severity: medium
- Languages: all
For each behavior a change adds or fixes, check whether a test asserts it, and name the missing test case by its input and expected output. Pure refactors with existing coverage, documentation changes and configuration tweaks do not need new tests.
CQ-009: Test that cannot fail
- Severity: medium
- Languages: all
Look for tests with no assertion, assertions on a mock's own return value, assert on a constant, tests that catch the exception they should check for, and tests skipped or marked expected-to-fail without a reason. A smoke test whose only purpose is to import a module or start a process, and says so in its name, is not a violation.
CQ-010: Dead or unreachable code
- Severity: low
- Languages: all
- References: CWE-561, CWE-563
Look for functions, classes, branches and variables that nothing reads, code after an unconditional return or raise, commented-out code blocks, and feature flags that are always on or always off. Public API that external callers may use, and entry points registered by name (plugins, CLI commands, framework hooks), are not dead code.
CQ-011: Magic number or string
- Severity: low
- Languages: all
- References: CWE-1106
Look for unexplained numeric or string literals that encode a business rule, limit, timeout or status code and appear in more than one place. 0, 1, -1, empty strings, and literals whose meaning is obvious from the line (range(len(x)), HTTP 404 next to a not-found message) are not violations.
CQ-012: Mutable default or shared state between calls
- Severity: high
- Languages: python, javascript, typescript
Look for mutable default arguments (Python only: def f(x=[]), def f(opts={})), class-level lists or dicts mutated through instances, and closures in loops over a var variable (JavaScript) or late-bound closures (Python) that capture the loop variable by reference. A mutable default that is never mutated and only read is low.
CQ-013: Misleading name or comment
- Severity: low
- Languages: all
- References: CWE-1116
Look for names whose meaning contradicts what the code does (a get_ function that writes, a boolean named like a count), and comments or docstrings that describe behavior the code no longer has. Short names in small scopes (i, k, v, e) are not violations.
CQ-014: Leftover debugging or temporary code
- Severity: low
- Languages: all
Look for print, console.log, debugger, breakpoint(), pdb.set_trace, dump, URLs pointing to localhost, and TODO or FIXME comments added by the change. Output that is the program's real interface (a CLI writing its result to stdout) and structured logging calls are not violations.
CQ-015: Too many parameters
- Severity: low
- Languages: all
- References: CWE-1064
Look for functions taking more than five parameters, or several boolean flags that change behavior, and suggest an options object or separate functions. Constructors of plain data classes and framework callbacks with a fixed signature are not violations.
CQ-016: Concurrency hazard
- Severity: high
- Languages: all
- References: CWE-362, CWE-367
Look for check-then-act sequences on shared data (read, test, write) without a lock or atomic operation, non-awaited coroutines or promises, async callbacks passed where a synchronous one is expected (such as forEach), and shared collections mutated from several threads. Code that runs in one thread or one event loop with no shared state is not a violation.
CQ-017: Destructive or multi-step write without safeguard
- Severity: high
- Languages: all
Look for DELETE or UPDATE statements without a WHERE clause, migrations that drop a column or table, and related writes (a debit and its credit, an order and its lines) done outside a transaction. Raise it to critical when the loss cannot be undone, such as data dropped with no backup, soft delete or reverse migration. Statements on a temporary table, and related writes wrapped in a transaction, are not violations.