6.0.0
What's Changed
This major release brings the cognitive complexity algorithm into full conformance with the SonarSource white paper (v1.7), correcting several long-standing divergences from the specification. The fix touches every match, try/except, with, comprehension, lambda, and recursive call in every analyzed file, so all users should expect score changes across their codebases. The module structure has also been refactored to separate algorithm logic from path orchestration, improving maintainability and fixing a pre-existing wasm build failure.
Breaking Changes
All of the following correct algorithm behaviour that diverged from the SonarSource specification. After upgrading, you will see different cognitive complexity scores for any file using these constructs. In most cases scores will increase, reflecting previously missed complexity.
Migration: Re-run
complexipyon your codebase after upgrading and review the new scores. If you use--max-complexity/--failedin CI, you will likely need to raise thresholds to accommodate the corrected scores. We recommend running in--quietmode first to see the overall delta before tightening thresholds again.
matchstatements — now correctly apply a structural + nesting increment (B1/B2/B3) per the paper, instead of being scored as 0 (#192)try/else/finallyblocks — body collected at current nesting level instead of+1, matching the paper's rule that "try and finally blocks are ignored altogether" (#192)excepthandlers — now charge1 + nesting_level(B3 nesting increment) instead of a flat+1(#192)withblocks — body no longer incorrectly raises the nesting level (#192)- Direct recursion — added scope-aware
RecursionFindervisitor that emits+1for each self-call, correctly skipping nested function/class definitions and lambdas to avoid false positives (#192) - Lambda expressions — added
Expr::Lambdaarm in boolean-op counting, recursing into the body atnesting_level + 1(#192) - Comprehensions — added
ListComp,SetComp,DictComp, andGeneratorarms; each generator charges1 + nesting_level, eachiffilter charges+1(#192) - Nested ternaries —
Expr::Ifbranches now recurse atnesting_level + 1so inner ternaries receive the correct nesting increment (#192) for/whileelseclauses — collected at the loop's own nesting level, not+1(#192)- Bare expression statements — added
Stmt::Exprarm to count boolean ops in standalone expressions (e.g.foo(a and b)) (#192) foriterable — added boolean-op counting on the iterable expression for parity withwhile(#192)
Changed
- Module structure — extracted all path orchestration (file I/O, directory traversal, URL cloning) from
cognitive_complexity.rsinto a newsrc/runner.rsmodule, gated#[cfg(feature = "python")], which also resolves a pre-existing wasm build failure caused by python-only symbols leaking into the wasm feature (#192) - Internal helpers — extracted
push_line,absorb,absorb_with_regions,finalize_region,count_line_bool_ops,loop_complexity,is_ignored, andanalyze_functionhelpers to eliminate the repeated fold/push pattern across all statement arms (#192) loop_complexityunification — unified structurally identicalStmt::ForandStmt::Whilehandling into a singleloop_complexitycall (#192)- Collapsed leaf arms — collapsed 7 near-identical statement arms (
Assign,AnnAssign,AugAssign,Return,Raise,Assert,Expr) throughcount_line_bool_ops(#192) Defaulton region types — derivedDefaultonComplexityRegionandRegionKindto eliminate repeatedelif_count: 0, case_count: 0, ...noise across every structural arm (#192)- Removed
merge_child— eliminatedmerge_childand its internalchild.regions.clone()as regions never needed the double-propagation (#192) - Windows CI hardening — hardened the Windows release unit-test job against transient Cargo HTTP/2 framing layer failures by disabling multiplexing and increasing retries (#189)
Tests
- Updated existing test assertions to match corrected scores:
test_match(0→1),test_recursive(0→1),test_try_nested(12→10), directory totals adjusted accordingly (#192) - Added
TestPaperConformanceclass with 14 targeted tests covering each fixed algorithm rule, including recursion regression tests for nested-function and lambda edge cases (#192)
Chores
- Added
[tool.uv] cache-keystopyproject.tomlto improve uv cache invalidation accuracy (#192)
PRs
- fix(complexity): align cognitive complexity algorithm with SonarSource paper and refactor module structure by @rohaquinlop in #192
- ci: Stabilize Windows release unit-test job by hardening Cargo network behavior by @copilot-swe-agent in #189
Full Changelog: 5.6.1...6.0.0